任意对象数组ArrayList的排序法(可自定义排序字段、排序方向)
ArrayList可以用来组合任何的对象,但其默认的排序是按对象本身值来排序,而不是应用自定义的,因此需要重新修改ArrayList的Sort方法的IComparer接口实现。以下是重新写的IComparer的实现,用了类型反射以做到能适合所有类的排序
性能测试:(仅供参考)
配置:PIII 733 384M内存
Windows2000SP4 .net FrameWork 1.1.4322 SP1
ArrayList容纳100个对象,排序时间大约10~20毫秒
ArrayList容纳1000个对象,排序时间大约100毫秒左右
ArrayList容纳10000个对象,排序时间大约1200~1600毫秒
代码:
ReverserClass
实现IComparer接口的自定义类,其中定义了ArrayList包含的对象的类型type和需要排序的类型的字段name
测试代码
UserClass
main
其中ReverserClass reverser = new ReverserClass(user.GetType(), "Name"); 中的要传递ArrayList数组中的对象的类型和需要作为排序依据的字段名称!
性能测试:(仅供参考)
配置:PIII 733 384M内存
Windows2000SP4 .net FrameWork 1.1.4322 SP1
ArrayList容纳100个对象,排序时间大约10~20毫秒
ArrayList容纳1000个对象,排序时间大约100毫秒左右
ArrayList容纳10000个对象,排序时间大约1200~1600毫秒
代码:
ReverserClass
实现IComparer接口的自定义类,其中定义了ArrayList包含的对象的类型type和需要排序的类型的字段name
public class ReverserClass : IComparer {
Type type = null;
string name = string.Empty;
string direction = "ASC";
public ReverserClass(Type type, string name, string direction) {
this.type = type;
this.name = name;
if(direction.Equals("DESC"))
this.direction = "DESC";
}
int IComparer.Compare( object x, object y ) {
object x1 = this.type.InvokeMember(this.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, x, null);
object y1 = this.type.InvokeMember(this.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, y, null);
if(direction.Equals("DESC"))
Swap(ref x1, ref y1);
return( (new CaseInsensitiveComparer()).Compare( x1, y1 ));
}
void Swap(ref object x, ref object y ) {
object temp = null;
temp = x;
x = y;
y = temp;
}
}
Type type = null;
string name = string.Empty;
string direction = "ASC";
public ReverserClass(Type type, string name, string direction) {
this.type = type;
this.name = name;
if(direction.Equals("DESC"))
this.direction = "DESC";
}
int IComparer.Compare( object x, object y ) {
object x1 = this.type.InvokeMember(this.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, x, null);
object y1 = this.type.InvokeMember(this.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, y, null);
if(direction.Equals("DESC"))
Swap(ref x1, ref y1);
return( (new CaseInsensitiveComparer()).Compare( x1, y1 ));
}
void Swap(ref object x, ref object y ) {
object temp = null;
temp = x;
x = y;
y = temp;
}
}
测试代码
UserClass
public class UserClass {
protected string _name;
protected int _age;
protected string _address;
public UserClass(string _name, int _age, string _address) {
this._name = _name;
this._age = _age;
this._address = _address;
}
public string Name {
get { return _name; }
set { _name = value; }
}
public int Age {
get { return _age; }
set { _age = value; }
}
public string Address {
get { return _address; }
set { _address = value; }
}
}
protected string _name;
protected int _age;
protected string _address;
public UserClass(string _name, int _age, string _address) {
this._name = _name;
this._age = _age;
this._address = _address;
}
public string Name {
get { return _name; }
set { _name = value; }
}
public int Age {
get { return _age; }
set { _age = value; }
}
public string Address {
get { return _address; }
set { _address = value; }
}
}
main
其中ReverserClass reverser = new ReverserClass(user.GetType(), "Name"); 中的要传递ArrayList数组中的对象的类型和需要作为排序依据的字段名称!
static void Main(string[] args)
{
UserClass user;
ArrayList ar = new ArrayList();
user = new UserClass("1", 1, "1");
ar.Add(user);
user = new UserClass("9", 9, "9");
ar.Add(user);
user = new UserClass("4", 4, "4");
ar.Add(user);
user = new UserClass("3", 3, "3");
ar.Add(user);
user = new UserClass("6", 6, "6");
ar.Add(user);
Console.WriteLine();
ReverserClass reverser = new ReverserClass(user.GetType(), "Name", "DESC");
ar.Sort(reverser);
Console.WriteLine();
}
{
UserClass user;
ArrayList ar = new ArrayList();
user = new UserClass("1", 1, "1");
ar.Add(user);
user = new UserClass("9", 9, "9");
ar.Add(user);
user = new UserClass("4", 4, "4");
ar.Add(user);
user = new UserClass("3", 3, "3");
ar.Add(user);
user = new UserClass("6", 6, "6");
ar.Add(user);
Console.WriteLine();
ReverserClass reverser = new ReverserClass(user.GetType(), "Name", "DESC");
ar.Sort(reverser);
Console.WriteLine();
}