[C#] C#对集合类进行快速排序 →→→→→进入此内容的聊天室

来自 , 2021-03-05, 写在 C#, 查看 124 次.
URL http://www.code666.cn/view/f0f800c9
  1. /// <summary>
  2.    /// 对集合进行排序,如
  3.    /// List<User> users=new List<User>(){.......}
  4.    /// ListSorter.SortList<list<User>,User>(ref users,"Name",SortDirection.Ascending);
  5.    /// </summary>
  6.    public class ListSorter
  7.    {
  8.        public static void SortList<TCollection, TItem>(ref TCollection list, string property, SortDirection direction) where TCollection : IList<TItem>
  9.        {
  10.            PropertyInfo[] propertyinfos = typeof(TItem).GetProperties();
  11.            foreach (PropertyInfo propertyinfo in propertyinfos)
  12.            {
  13.                if (propertyinfo.Name == property)          //取得指定的排序属性
  14.                {
  15.                    QuickSort<TCollection, TItem>(ref list, 0, list.Count - 1, propertyinfo, direction);
  16.                }
  17.            }
  18.        }
  19.        /// <summary>
  20.        /// 快速排序算法
  21.        /// </summary>
  22.        /// <typeparam name="TCollection">集合类型,需要实现Ilist<T>集合</typeparam>
  23.        /// <typeparam name="TItem">集合中对象的类型</typeparam>
  24.        /// <param name="list">集合对象</param>
  25.        /// <param name="left">起始位置,从0开始</param>
  26.        /// <param name="right">终止位置</param>
  27.        /// <param name="propertyinfo">集合中对象的属性,属性必须要实现IComparable接口</param>
  28.        /// <param name="direction">排序类型(升序或降序)</param>
  29.        private static void QuickSort<TCollection, TItem>(ref TCollection list, int left, int right, PropertyInfo propertyinfo, SortDirection direction) where TCollection : IList<TItem>
  30.        {
  31.            if (left < right)
  32.            {
  33.                int i = left, j = right;
  34.                TItem key = list[left];
  35.                while (i < j)
  36.                {
  37.                    if (direction == SortDirection.Ascending)
  38.                    {
  39.                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) < 0)
  40.                        {
  41.                            j--;
  42.                        }
  43.                        if (i < j)
  44.                        {
  45.                            list[i] = list[j];
  46.                            i++;
  47.                        }
  48.  
  49.                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) > 0)
  50.                        {
  51.                            i++;
  52.                        }
  53.                        if (i < j)
  54.                        {
  55.                            list[j] = list[i];
  56.                            j--;
  57.                        }
  58.                        list[i] = key;
  59.                    }
  60.                    else
  61.                    {
  62.                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) > 0)
  63.                        {
  64.                            j--;
  65.                        }
  66.                        if (i < j)
  67.                        {
  68.                            list[i] = list[j];
  69.                            i++;
  70.                        }
  71.  
  72.                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) < 0)
  73.                        {
  74.                            i++;
  75.                        }
  76.                        if (i < j)
  77.                        {
  78.                            list[j] = list[i];
  79.                            j--;
  80.                        }
  81.                        list[i] = key;
  82.                    }
  83.                }
  84.                //执行递归调用
  85.                QuickSort<TCollection, TItem>(ref list, left, i - 1, propertyinfo, direction);
  86.                QuickSort<TCollection, TItem>(ref list, i + 1, right, propertyinfo, direction);
  87.            }
  88.        }
  89.    }
  90.    /// <summary>
  91.    /// 排序类型
  92.    /// </summary>
  93.    public enum SortDirection
  94.    {
  95.        Ascending,
  96.        Descending
  97.    }
  98. //csharp/6832

回复 "C#对集合类进行快速排序"

这儿你可以回复上面这条便签

captcha