柔城

SOSOFT articles

C#将集合快速排序

C#实现集合排序类。

 说明:

1、集合类型参数化;

2、可根据集合中的对象的各个属性进行排序,传入属性名称即可;

注:属性必须实现了IComparable接口,C#中int、datetime、string等基本类型都已经实现了IComparable接口。

 

 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              // http://www.cnblogs.com/sosoft/
15                 {
16                     QuickSort<TCollection, TItem>(ref list, 0, list.Count - 1, propertyinfo, direction);
17                 }
18             }
19         }
20         /// <summary>
21         /// 快速排序算法
22         /// </summary>
23         /// <typeparam name="TCollection">集合类型,需要实现Ilist<T>集合</typeparam>
24         /// <typeparam name="TItem">集合中对象的类型</typeparam>
25         /// <param name="list">集合对象</param>
26         /// <param name="left">起始位置,从0开始</param>
27         /// <param name="right">终止位置</param>
28         /// <param name="propertyinfo">集合中对象的属性,属性必须要实现IComparable接口</param>
29         /// <param name="direction">排序类型(升序或降序)</param>
30         private static void QuickSort<TCollection, TItem>(ref TCollection list, int left, int right, PropertyInfo propertyinfo, SortDirection direction) where TCollection : IList<TItem>
31         {
32             if (left < right)
33             {
34                 int i = left, j = right;
35                 TItem key = list[left];
36                 while (i < j)
37                 {
38                     if (direction == SortDirection.Ascending)
39                     {
40                         while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) < 0)
41                         {
42                             j--;
43                         }
44                         if (i < j)
45                         {
46                             list[i] = list[j];
47                             i++;
48                         }
49 
50                         while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) > 0)
51                         {
52                             i++;
53                         }
54                         if (i < j)
55                         {
56                             list[j] = list[i];
57                             j--;
58                         }
59                         list[i] = key;
60                     }
61                     else
62                     {
63                         while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) > 0)
64                         {
65                             j--;
66                         }
67                         if (i < j)
68                         {
69                             list[i] = list[j];
70                             i++;
71                         }
72 
73                         while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) < 0)
74                         {
75                             i++;
76                         }
77                         if (i < j)
78                         {
79                             list[j] = list[i];
80                             j--;
81                         }
82                         list[i] = key;
83                     }
84                 }
85                 //执行递归调用 
86                 QuickSort<TCollection, TItem>(ref list, left, i - 1, propertyinfo, direction);
87                 QuickSort<TCollection, TItem>(ref list, i + 1, right, propertyinfo, direction);
88             }
89         }
90     }
91     /// <summary>
92     /// 排序类型
93     /// </summary>
94     public enum SortDirection
95     {
96         Ascending,
97         Descending
98     }

 

posted on 2013-12-11 18:06  柔城  阅读(2416)  评论(1编辑  收藏  举报

导航