任意对象数组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
ExpandedBlockStart.gif ContractedBlock.gif public   class  ReverserClass : IComparer   dot.gif {
InBlock.gif        Type type 
= null;
InBlock.gif        
string name = string.Empty;
InBlock.gif        
string direction = "ASC";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public ReverserClass(Type type, string name, string direction) dot.gif{
InBlock.gif            
this.type = type;
InBlock.gif            
this.name = name;
InBlock.gif            
if(direction.Equals("DESC"))
InBlock.gif                
this.direction = "DESC";
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
int IComparer.Compare( object x, object y )  dot.gif{
InBlock.gif             
object x1 = this.type.InvokeMember(this.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, x, null);
InBlock.gif             
object y1 = this.type.InvokeMember(this.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, y, null);
InBlock.gif             
if(direction.Equals("DESC"))
InBlock.gif                 Swap(
ref x1, ref y1);
InBlock.gif             
return( (new CaseInsensitiveComparer()).Compare( x1, y1 ));
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
void Swap(ref object x, ref object y ) dot.gif{
InBlock.gif            
object temp = null;
InBlock.gif            temp 
= x;
InBlock.gif            x 
= y;
InBlock.gif            y 
= temp;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }


测试代码
UserClass
ExpandedBlockStart.gif ContractedBlock.gif public   class  UserClass  dot.gif {
InBlock.gif    
protected string _name;
InBlock.gif    
protected int _age;
InBlock.gif    
protected string _address;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public UserClass(string _name, int _age, string _address) dot.gif{
InBlock.gif        
this._name = _name;
InBlock.gif        
this._age = _age;
InBlock.gif        
this._address = _address;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string Name dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _name = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public int Age dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _age; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _age = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string Address dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _address; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _address = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

main
其中ReverserClass reverser =   new  ReverserClass(user.GetType() " Name " ); 中的要传递ArrayList数组中的对象的类型和需要作为排序依据的字段名称!
None.gif          static   void  Main( string [] args)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            UserClass user;
InBlock.gif            ArrayList ar 
= new ArrayList();
InBlock.gif            user 
= new UserClass("1"1"1");
InBlock.gif            ar.Add(user);
InBlock.gif            user 
= new UserClass("9"9"9");
InBlock.gif            ar.Add(user);
InBlock.gif            user 
= new UserClass("4"4"4");
InBlock.gif            ar.Add(user);
InBlock.gif            user 
= new UserClass("3"3"3");
InBlock.gif            ar.Add(user);
InBlock.gif            user 
= new UserClass("6"6"6");
InBlock.gif            ar.Add(user);
InBlock.gif            
InBlock.gif            Console.WriteLine(dot.gif);
InBlock.gif            ReverserClass reverser 
= new ReverserClass(user.GetType(), "Name", "DESC");
InBlock.gif            ar.Sort(reverser);
InBlock.gif            Console.WriteLine(dot.gif);
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值