要对自定义类数组或List进行排序,譬如:

List<User> userList;

ArrayList arrayList;

最重要的是:继承IComparer<T>接口,实现int IComparer<T>.Compare(T t1, T t2)方法。

代码如下:

 

C#中List的排序(Sort)

上面主要是运用了 C#的反射 和 Framework中的排序算法。

像上面那样实现接口后,就可以使用List<T>进行 升序/降序 排序了。

测试代码如下:

 

C#中List的排序(Sort)
C#中List的排序(Sort)
using System;
C#中List的排序(Sort)
using System.Collections.Generic;
C#中List的排序(Sort)
using System.Collections;
C#中List的排序(Sort)
using System.Reflection;
C#中List的排序(Sort)
using System.Text;
C#中List的排序(Sort)
C#中List的排序(Sort)
namespace List_T_SortTest_u_2

 

相关文章: