加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 17:55:42 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 C#对集合类进行快速排序 /// summary /// 对集合进行排序,如 /// ListUser users=new ListUser(){.......} /// ListSorter.SortListlistUser,User(re

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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,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,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)) > 0)
                       {
                           j--;
                       }
                       if (i < j)
                       {
                           list[i] = list[j];
                           i++;
                       }
  
                       while (i < j && ((IComparable)propertyinfo.GetValue(key,null)) < 0)
                       {
                           i++;
                       }
                       if (i < j)
                       {
                           list[j] = list[i];
                           j--;
                       }
                       list[i] = key;
                   }
               }
               //执行递归调用
               QuickSort<TCollection,left,i - 1,direction);
               QuickSort<TCollection,i + 1,right,direction);
           }
       }
   }
   /// <summary>
   /// 排序类型
   /// </summary>
   public enum SortDirection
   {
       Ascending,Descending
   }

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读