通过写一个Demo展示C#中多种常用的集合排序方法

不多说,程序很简单,就是将集合中的数据进行排序,但使用到的知识点还是比较多的,大牛勿喷,谨献给初学者!直接上程序吧!

复制代码
namespace Demo
{
    /// <summary>
    /// Demo:使用不同排序方法对元素进行排序
    /// </summary>
    class Program
    {
        private static void Main(string[] args)
        {

            ArrayList arrayList = Product.GetArrayList();
            List<Product> list = Product.GetList();


            //1、使用继承IComparer接口的函数来进行排序
            arrayList.Sort(new ProductCompare());
            foreach (Product product in arrayList)
            {
                Console.WriteLine(product.ToString());
            }
            Console.WriteLine("---------------------------");


            //2、使用继承IComparer<T>接口的函数来进行排序
            list.Sort(new ProductCompareT());
            foreach (Product product in list)
            {
                Console.WriteLine(product.ToString());
            }
            Console.WriteLine("---------------------------");

            
            //3、使用委托来进行排序
            list.Sort(delegate(Product x, Product y)
            {
                return x.Price.CompareTo(y.Price);
            });
            foreach (Product product in list)
            {
                Console.WriteLine(product.ToString());
            }

            //4、使用Lambda表达式来进行排序;
            list.Sort((x, y) => x.Price.CompareTo(y.Price));
            foreach (Product product in list)
            {
                Console.WriteLine(product.ToString());
            }


            //5、使用扩展方法来进行排序
            foreach (Product product in list.OrderBy(p=>p.Price))
            {
                Console.WriteLine(product.ToString());
            }
            Console.ReadKey();
        }
    }

    public class Product
    {
        public string Name { get; set; }

        public decimal Price { get; set; }

        public static ArrayList GetArrayList()
        {
            return new ArrayList()
            {
                new Product {Name = "WindowsPhone", Price = 10m},
                new Product {Name = "Apple", Price = 20m},
                new Product {Name = "Android", Price = 5m}
            };
        }

        public static List<Product> GetList()
        {
            return new List<Product>()
            {
                new Product {Name = "WindowsPhone", Price = 10m},
                new Product {Name = "Apple", Price = 20m},
                new Product {Name = "Android", Price = 5m}
            };
        }

        public override string ToString()
        {
            return String.Format("{0}--{1}", Name, Price);
        }
    }

    /// <summary>
    /// 使用IComparer对ArrayList进行排序
    /// 显示实现Compare接口,常用ArrayList类型的集合来调用
    /// </summary>
    public class ProductCompare : IComparer
    {
        public int Compare(object x, object y)
        {
            Product first = x as Product;
            Product second = y as Product;
            if (first != null && second !=null)
            {
                return first.Price.CompareTo(second.Price);
            }
            else
            {
                return -1;
            }
        }
    }

    /// <summary>
    /// 使用IComparer<Product>进行排序
    /// 显式实现Compare接口,常用List<T>类型的集合来调用
    /// </summary>
    public class ProductCompareT : IComparer<Product>
    {
        public int Compare(Product x, Product y)
        {
            Product first = x as Product;
            Product second = y as Product;
            if (first != null && second != null)
            {
                return first.Price.CompareTo(second.Price);
            }
            else
            {
                return -1;
            }
        }
    }
}
复制代码

 

作者:hippiezhou

出处:https://www.cnblogs.com/hippieZhou/p/4528553.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

Find Anyway

posted @   hippieZhou  阅读(2027)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up dark_mode palette
选择主题