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
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class ReverserClass : IComparer
{
Type type = null;
string name = string.Empty;
string direction = "ASC";
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public ReverserClass(Type type, string name, string direction)
{
this.type = type;
this.name = name;
if(direction.Equals("DESC"))
this.direction = "DESC";
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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 ));
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void Swap(ref object x, ref object y )
{
object temp = null;
temp = x;
x = y;
y = temp;
}
} 测试代码 UserClass
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class UserClass
{
protected string _name;
protected int _age;
protected string _address;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public UserClass(string _name, int _age, string _address)
{
this._name = _name;
this._age = _age;
this._address = _address;
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _name; }
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _name = value; }
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Age
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _age; }
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _age = value; }
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Address
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _address; }
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _address = value; }
}
} main 其中ReverserClass reverser = new ReverserClass(user.GetType(), "Name"); 中的要传递ArrayList数组中的对象的类型和需要作为排序依据的字段名称!
static void Main(string[] args)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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(
);
}
阅读(1868) | 评论(2) | 转发(0) |