cxi集合の高速ソート類の実現コード共有


説明:
1、集合タイプのパラメータ化。
2、セット内のオブジェクトの各属性に従って並べ替えられ、属性名を入力すれば良い。
注:属性はIComparableインターフェースを実現しなければなりません。C〓中int、datetime、stringなどの基本タイプはすでにIComparableインターフェースを実現しました。

/// <summary>
    /// ,
    /// List<User> users=new List<User>(){.......}
    /// ListSorter.SortList<list<User>,User>(ref users,"Name",SortDirection.Ascending);
    /// </summary>
    public class ListSorter
    {
        public static void SortList<TCollection, TItem>(ref TCollection list, string property, SortDirection direction) where TCollection : IList<TItem>
        {
            PropertyInfo[] propertyinfos = typeof(TItem).GetProperties();
            foreach (PropertyInfo propertyinfo in propertyinfos)
            {
                if (propertyinfo.Name == property)          //
             // http://www.cnblogs.com/sosoft/
                {
                    QuickSort<TCollection, TItem>(ref list, 0, list.Count - 1, propertyinfo, direction);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TCollection"> , Ilist<T> </typeparam>
        /// <typeparam name="TItem"> </typeparam>
        /// <param name="list"> </param>
        /// <param name="left"> , 0 </param>
        /// <param name="right"> </param>
        /// <param name="propertyinfo"> , IComparable </param>
        /// <param name="direction"> ( )</param>
        private static void QuickSort<TCollection, TItem>(ref TCollection list, int left, int right, PropertyInfo propertyinfo, SortDirection direction) where TCollection : IList<TItem>
        {
            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<TCollection, TItem>(ref list, left, i - 1, propertyinfo, direction);
                QuickSort<TCollection, TItem>(ref list, i + 1, right, propertyinfo, direction);
            }
        }
    }
    /// <summary>
    ///
    /// </summary>
    public enum SortDirection
    {
        Ascending,
        Descending
    }