由于项目的原因用到了List<T> 泛型,Framework都已经到了3.5了。可是我一直都没有正式的用过2.0很是遗憾。
特别是对泛型更是一知半解,今天又弄了些资料觉得挺有用就收集到博客上来了。
闲话少叙,今天用到的List<T>的Sort功能纯属是从高人那里得来的,只是进行了少量的改动而已。
要对自定义类数组或List进行排序,譬如:
List<User> userList;
ArrayList arrayList;
最重要的是:继承IComparer<T>接口,实现int IComparer<T>.Compare(T t1, T t2)方法。
代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 继承IComparer<T>接口,实现同一自定义类型 对象比较
/// </summary>
/// <typeparam name="T">T为泛用类型</typeparam>
public class Reverser<T> : IComparer<T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
private Type type = null;
private ReverserInfo info;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="type">进行比较的类类型</param>
/// <param name="name">进行比较对象的属性名称</param>
/// <param name="direction">比较方向(升序/降序)</param>
public Reverser(Type type, string name, ReverserInfo.Direction direction)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.type = type;
this.info.name = name;
if (direction != ReverserInfo.Direction.ASC)
this.info.direction = direction;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="className">进行比较的类名称</param>
/// <param name="name">进行比较对象的属性名称</param>
/// <param name="direction">比较方向(升序/降序)</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public Reverser(string className, string name, ReverserInfo.Direction direction) ...{
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.type = Type.GetType(className, true);
this.info.name = name;
this.info.direction = direction;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
catch (Exception e)...{
throw new Exception(e.Message);
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="t">进行比较的类型的实例</param>
/// <param name="name">进行比较对象的属性名称</param>
/// <param name="direction">比较方向(升序/降序)</param>
public Reverser(T t, string name, ReverserInfo.Direction direction)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.type = t.GetType();
this.info.name = name;
this.info.direction = direction;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//必须!实现IComparer<T>的比较方法。
int IComparer<T>.Compare(T t1, T t2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
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);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//交换操作数
private void Swap(ref object x, ref object y)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
object temp = null;
temp = x;
x = y;
y = temp;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 对象比较时使用的信息类
/// </summary>
public struct ReverserInfo
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 比较的方向,如下:
/// ASC:升序
/// DESC:降序
/// </summary>
public enum Direction
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
ASC = 0,
DESC,
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public enum Target
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
CUSTOMER = 0,
FORM,
FIELD,
SERVER,
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string name;
public Direction direction;
public Target target;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
上面主要是运用了 C#的反射 和 Framework中的排序算法。
像上面那样实现接口后,就可以使用List<T>进行 升序/降序 排序了。
测试代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
using System.Collections.Generic;
using System.Collections;
using System.Reflection;
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace List_T_SortTest_u_2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
测试Reverser代码段#region 测试Reverser<T>代码段
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 实体类User,测试用
/// </summary>
public class User
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
protected string _name;
protected int _age;
protected string _address;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public User(string name, int age, string address)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this._name = name;
this._age = age;
this._address = address;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string Name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return _name; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set ...{ _name = value; }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int Age
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return _age; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set ...{ _age = value; }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string Address
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return _address; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set ...{ _address = value; }
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 主程序类(启动类),测试用
/// </summary>
class Program
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
List<User> userList = new List<User>();
User user;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
user = new User("Wang", 21, "ShenYang");
userList.Add(user);
user = new User("Yan", 27, "JinZhou");
userList.Add(user);
user = new User("Liu", 26, "BeiJing");
userList.Add(user);
user = new User("Zhao", 30, "ChaoYang");
userList.Add(user);
user = new User("Yang", 27, "FuXin");
userList.Add(user);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//for (int i = 0; i < ar.Count; i++ )
// ;
Console.Write("Name ");
Console.Write("Age ");
Console.Write("Address " + " " + " ");
Console.WriteLine("-----------------------");
foreach (User u in userList)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Console.Write(u.Name + " ");
Console.Write(u.Age + " ");
Console.Write(u.Address + " " + " ");
}
Console.WriteLine();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Reverser<User> reverser = new Reverser<User>(user.GetType(), "Name", ReverserInfo.Direction.DESC);
userList.Sort(reverser);
Console.WriteLine();
foreach (User u in userList)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Console.Write(u.Name + " ");