List.Sort()如何使用提供的Comparer实例?

时间:2011-11-27 15:42:37

标签: c# sorting c#-4.0 c#-3.0

在下面的代码中,主方法的排序方法会发生什么,它是否包含循环?

我发现它使用QuickSort Algorithm但是如何按比较函数排序?

class Products
    {
        public string Name { get; private set; }
        public decimal Price { get; private set; }
        public Products(string name,decimal price)
        {
            Name = name;
            Price = price;
        }
        public Products()
        {

        }
        public static List<Products> GetSampleProducts()
        {
            return new List<Products>()
            {
                new Products{Name = "Company",Price=1800},
                new Products{Name = "Assassins",Price=2800},
                new Products{Name = "Zrogs",Price=1300},
                new Products{Name = "Swenney Todd",Price=1300}
            };
        }
        public override string ToString()
        {
            return string.Format("{0} price is {1}", Name, Price);
        }
    }
    class ProductComparer : IComparer<Products>
    {
        public int Compare(Products x, Products y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Products products = new Products();
            List<Products> lst_products = Products.GetSampleProducts();
            lst_products.Sort (new ProductComparer());
            foreach (Products p in lst_products)
            {
                Console.WriteLine(p);
            }
            Console.ReadKey();
        }
    }

...谢谢

1 个答案:

答案 0 :(得分:2)

一般情况下,它使用Array.Sort()方法和比较器来比较每个接下来的两个项目,同时排序uisng QuickSort算法。

有趣的动画QuickSort:

快速排序算法的可视化。水平线是枢轴值: enter image description here

修改

以下是使用提供的比较器(QuickSort,SwapIfGreaterWithItems)实际执行QuickSort的方法

// internal class ArraySortHelper<T> : IArraySortHelper<T>
internal static void QuickSort(
                         T[] keys, 
                         int left, 
                         int right, 
                         IComparer<T> comparer)
{
    do
    {
        int a = left;
        int b = right;
        int num3 = a + ((b - a) >> 1);
        ArraySortHelper<T>.SwapIfGreaterWithItems(keys, comparer, a, num3);
        ArraySortHelper<T>.SwapIfGreaterWithItems(keys, comparer, a, b);
        ArraySortHelper<T>.SwapIfGreaterWithItems(keys, comparer, num3, b);
        T y = keys[num3];
        do
        {
            while (comparer.Compare(keys[a], y) < 0)
            {
                a++;
            }
            while (comparer.Compare(y, keys[b]) < 0)
            {
                b--;
            }
            if (a > b)
            {
                break;
            }
            if (a < b)
            {
                T local2 = keys[a];
                keys[a] = keys[b];
                keys[b] = local2;
            }
            a++;
            b--;
        }
        while (a <= b);
        if ((b - left) <= (right - a))
        {
            if (left < b)
            {
                ArraySortHelper<T>.QuickSort(keys, left, b, comparer);
            }
            left = a;
        }
        else
        {
            if (a < right)
            {
                ArraySortHelper<T>.QuickSort(keys, a, right, comparer);
            }
            right = b;
        }
    }
    while (left < right);
}


private static void SwapIfGreaterWithItems(
                                T[] keys, 
                                IComparer<T> comparer, 
                                int a, 
                                int b)
{
    if ((a != b) && (comparer.Compare(keys[a], keys[b]) > 0))
    {
        T local = keys[a];
        keys[a] = keys[b];
        keys[b] = local;
    }
}