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

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
C#对集合类进行快速排序
/// <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)          //取得指定的排序属性
               {
                   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
   }

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:C#从SqlServer数据库读写文件代码

下一篇:php的zip压缩代码