中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C#對集合類進(jìn)行快速排序

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
C#對集合類進(jìn)行快速排序
/// <summary>
   /// 對集合進(jìn)行排序,如
   /// 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)          //取得指定的排序?qū)傩?               {
                   QuickSort<TCollection, TItem>(ref list, 0, list.Count - 1, propertyinfo, direction);
               }
           }
       }
       /// <summary>
       /// 快速排序算法
       /// </summary>
       /// <typeparam name="TCollection">集合類型,需要實現(xiàn)Ilist<T>集合</typeparam>
       /// <typeparam name="TItem">集合中對象的類型</typeparam>
       /// <param name="list">集合對象</param>
       /// <param name="left">起始位置,從0開始</param>
       /// <param name="right">終止位置</param>
       /// <param name="propertyinfo">集合中對象的屬性,屬性必須要實現(xiàn)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;
                   }
               }
               //執(zhí)行遞歸調(diào)用
               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
   }

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:C#從SqlServer數(shù)據(jù)庫讀寫文件代碼

下一篇:php的zip壓縮代碼