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);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构