深入理解 c# 第一章 使用IComparer委托 对 List 进行排序

    class ListSortWithComparisonDelegate
    {
        static void Main()
        {
            List products = Product.GetSampleProducts();
            products.Sort(delegate(Product first, Product second)
                { return first.Name.CompareTo(second.Name); }
            );
            foreach (Product product in products)
            {
                Console.WriteLine(product);
            }
        }
    }

        public static List GetSampleProducts()
        {
            List list = new List();
            list.Add(new Product("West Side Story", 9.99m));
            list.Add(new Product("Assassins", 14.99m));
            list.Add(new Product("Frogs", 13.99m));
            list.Add(new Product("Sweeney Todd", 10.99m));
            return list;
        }


输入  

"West Side Story", 9.99m

"Assassins", 14.99m

"Frogs", 13.99m

"Sweeney Todd", 10.99m


输出

Assassins: 14.99
Frogs: 13.99
Sweeney Todd: 10.99
West Side Story: 9.99

用委托提供给sort方法做比较

你可能感兴趣的:(c#)