c#集合快速排序类实现代码分享

说明:

1、集合类型参数化;

2、可根据集合中的对象的各个属性进行排序,传入属性名称即可;

注:属性必须实现了IComparable接口,C#中int、datetime、string等基本类型都已经实现了IComparable接口。

复制代码 代码如下:

///
    /// 对集合进行排序,如
    /// List users=new List(){.......}
    /// ListSorter.SortList,User>(ref users,"Name",SortDirection.Ascending);
    ///

    public class ListSorter
    {
        public static void SortList(ref TCollection list, string property, SortDirection direction) where TCollection : IList
        {
            PropertyInfo[] propertyinfos = typeof(TItem).GetProperties();
            foreach (PropertyInfo propertyinfo in propertyinfos)
            {
                if (propertyinfo.Name == property)          //取得指定的排序属性
             // http://www.cnblogs.com/sosoft/
                {
                    QuickSort(ref list, 0, list.Count - 1, propertyinfo, direction);
                }
            }
        }
        ///
        /// 快速排序算法
        ///

        /// 集合类型,需要实现Ilist集合
        /// 集合中对象的类型
        /// 集合对象
        /// 起始位置,从0开始
        /// 终止位置
        /// 集合中对象的属性,属性必须要实现IComparable接口
        /// 排序类型(升序或降序)
        private static void QuickSort(ref TCollection list, int left, int right, PropertyInfo propertyinfo, SortDirection direction) where TCollection : IList
        {
            if (left < right)
            {
                int i = left, j = right;
                TItem key = list[left];
                while (i < j)
                {
                    if (direction == SortDirection.Ascending)
                    {
                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) < 0)
                        {
                            j--;
                        }
                        if (i < j)
                        {
                            list[i] = list[j];
                            i++;
                        }

                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) > 0)
                        {
                            i++;
                        }
                        if (i < j)
                        {
                            list[j] = list[i];
                            j--;
                        }
                        list[i] = key;
                    }
                    else
                    {
                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) > 0)
                        {
                            j--;
                        }
                        if (i < j)
                        {
                            list[i] = list[j];
                            i++;
                        }

                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) < 0)
                        {
                            i++;
                        }
                        if (i < j)
                        {
                            list[j] = list[i];
                            j--;
                        }
                        list[i] = key;
                    }
                }
                //执行递归调用
                QuickSort(ref list, left, i - 1, propertyinfo, direction);
                QuickSort(ref list, i + 1, right, propertyinfo, direction);
            }
        }
    }
    ///


    /// 排序类型
    ///

    public enum SortDirection
    {
        Ascending,
        Descending
    }

你可能感兴趣的:(c#集合快速排序类实现代码分享)