List<T> 排序
不说什么了,直接贴代码
1
//Compare records by selected column
2
internal class ListComparer<T> : IComparer<T>
3
{
4
private string propertyName;
5
6
public ListComparer(string PropertyName)
7
{
8
propertyName = PropertyName;
9
}
10
11
IComparer Members#region IComparer<TBaseBusinessObject> Members
12
13
public int Compare(T x, T y)
14
{
15
PropertyInfo property = typeof(T).GetProperty(propertyName);
16
if (property.PropertyType == Type.GetType("System.Int16"))
17
{
18
int xNumber = 0;
19
int yNumber = 0;
20
if (property.GetValue(x, null) != null)
21
{
22
xNumber = Convert.ToInt16(property.GetValue(x, null).ToString());
23
}
24
if (property.GetValue(y, null) != null)
25
{
26
yNumber = Convert.ToInt16(property.GetValue(y, null).ToString());
27
}
28
return xNumber.CompareTo(yNumber);
29
}
30
if (property.PropertyType == Type.GetType("System.Int32"))
31
{
32
int xNumber = 0;
33
int yNumber = 0;
34
if (property.GetValue(x, null) != null)
35
{
36
xNumber = Convert.ToInt32(property.GetValue(x, null).ToString());
37
}
38
if (property.GetValue(y, null) != null)
39
{
40
yNumber = Convert.ToInt32(property.GetValue(y, null).ToString());
41
}
42
return xNumber.CompareTo(yNumber);
43
}
44
if (property.PropertyType == Type.GetType("System.Decimal"))
45
{
46
double xNumber = 0;
47
double yNumber = 0;
48
if (property.GetValue(x, null) != null)
49
{
50
xNumber = Convert.ToDouble(property.GetValue(x, null).ToString());
51
}
52
if (property.GetValue(y, null) != null)
53
{
54
yNumber = Convert.ToDouble(property.GetValue(y, null).ToString());
55
}
56
return xNumber.CompareTo(yNumber);
57
}
58
if (property.PropertyType == Type.GetType("System.Double"))
59
{
60
double xNumber = 0;
61
double yNumber = 0;
62
if (property.GetValue(x, null) != null)
63
{
64
xNumber = Convert.ToDouble(property.GetValue(x, null).ToString());
65
}
66
if (property.GetValue(y, null) != null)
67
{
68
yNumber = Convert.ToDouble(property.GetValue(y, null).ToString());
69
}
70
return xNumber.CompareTo(yNumber);
71
}
72
if (property.PropertyType == Type.GetType("System.DateTime"))
73
{
74
DateTime xTime = DateTime.Now;
75
DateTime yTime = DateTime.Now;
76
if (property.GetValue(x, null) != null)
77
{
78
xTime = Convert.ToDateTime(property.GetValue(x, null).ToString());
79
}
80
if (property.GetValue(y, null) != null)
81
{
82
yTime = Convert.ToDateTime(property.GetValue(y, null).ToString());
83
}
84
return xTime.CompareTo(yTime);
85
}
86
if ((property.PropertyType == Type.GetType("System.String")) ||
87
(property.PropertyType == Type.GetType("System.Boolean")))
88
{
89
string xText = string.Empty;
90
string yText = string.Empty;
91
if (property.GetValue(x, null) != null)
92
{
93
xText = property.GetValue(x, null).ToString();
94
}
95
if (property.GetValue(y, null) != null)
96
{
97
yText = property.GetValue(y, null).ToString();
98
}
99
return xText.CompareTo(yText);
100
}
101
return 0;
102
103
}
104
105
#endregion
106
}
107
108
//Sort list by sort field and sortDirection.
109
public class SortList<T>
110
{
111
public static void Sort(List<T> list, string sortfield, bool isAscending)
112
{
113
ListComparer<T> listComparer = new ListComparer<T>(sortfield);
114
list.Sort(listComparer);
115
if (!isAscending) list.Reverse();
116
}
117
}
我们调用的时候,直接排序
SortList<Model>.Sort(IListModel as List<Model>, "Name", true);