由于项目的原因用到了List<T> 泛型,要对自定义类数组或List进行排序,譬如:
List<User> userList;
ArrayList arrayList;
最重要的是:继承IComparer<T>接口,实现int IComparer<T>.Compare(T t1, T t2)方法。
/// <summary>
/// 对象比较时使用的信息类
/// </summary>
public struct ReverserInfo
{
/// <summary>
/// 比较的方向,如下:
/// ASC:升序
/// DESC:降序
/// </summary>
public enum Direction
{
ASC = 0,
DESC =1,
};
/// <summary>
/// 字段名称
/// </summary>
public string name;
/// <summary>
/// 排序方式
/// </summary>
public Direction direction;
}
/// <summary>
/// 排序
/// </summary>
/// <typeparam name="T"></typeparam>
public class Reverser<T> : IComparer<T>
{
private Type type = null;
private ReverserInfo info;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="type">进行比较的类类型</param>
/// <param name="name">进行比较对象的属性名称</param>
/// <param name="direction">比较方向(升序/降序)</param>
public Reverser(Type type, string name, ReverserInfo.Direction direction)
{
this.type = type;
this.info.name = name;
if (direction != ReverserInfo.Direction.ASC)
this.info.direction = direction;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="className">进行比较的类名称</param>
/// <param name="name">进行比较对象的属性名称</param>
/// <param name="direction">比较方向(升序/降序)</param>
public Reverser(string className, string name, ReverserInfo.Direction direction)
{
try
{
this.type = Type.GetType(className, true);
this.info.name = name;
this.info.direction = direction;
}
catch (Exception e){
throw new Exception(e.Message);
}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="t">进行比较的类型的实例</param>
/// <param name="name">进行比较对象的属性名称</param>
/// <param name="direction">比较方向(升序/降序)</param>
public Reverser(T t, string name, ReverserInfo.Direction direction)
{
this.type = t.GetType();
this.info.name = name;
this.info.direction = direction;
}
/// <summary>
/// 实现IComparer的比较方法。
/// </summary>
/// <param name="t1"></param>
/// <param name="t2"></param>
/// <returns></returns>
int IComparer<T>.Compare(T t1, T t2)
{
object x = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null);
object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null);
if (this.info.direction != ReverserInfo.Direction.ASC)
Swap(ref x, ref y);
return (new CaseInsensitiveComparer()).Compare(x, y);
}
/// <summary>
/// 交换操作数
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
private void Swap(ref object x, ref object y)
{
object temp = null;
temp = x;
x = y;
y = temp;
}
}
使用样例
public string Test()
{
List<TagNameModel> TagNameLists = new List<TagNameModel>();
for (int i = 0; i < 5; i++)
{
TagNameModel tage = new TagNameModel() { BackgroundColor = "12", CategoryId = i, FontColor = "er", Name = i.ToString(), TagId = 5 - i};
TagNameLists.Add(tage);
}
//调用排序类,按照CategoryId字段,升序排列
Reverser<TagNameModel> reverser = new Reverser<TagNameModel>(new TagNameModel().GetType(), "CategoryId", ReverserInfo.Direction.ASC);
TagNameLists.Sort(reverser);
return TagNameLists.Serialize();
}