POOPE 发表于 2021-8-6 14:57:17

任意对象数组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

https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedBlock.gifpublic       class    ReverserClass : IComparer     https://blog.51cto.com/iteyer/Images/dot.gif   {
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif        Type type = null;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif        string name = string.Empty;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif        string direction = "ASC";
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif        public ReverserClass(Type type, string name, string direction) https://blog.51cto.com/iteyer/Images/dot.gif{
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            this.type = type;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            this.name = name;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            if(direction.Equals("DESC"))
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif                this.direction = "DESC";
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif         int IComparer.Compare( object x, object y )  https://blog.51cto.com/iteyer/Images/dot.gif{
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif             object x1 = this.type.InvokeMember(this.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, x, null);
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif             object y1 = this.type.InvokeMember(this.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, y, null);
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif             if(direction.Equals("DESC"))
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif                 Swap(ref x1, ref y1);
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif             return( (new CaseInsensitiveComparer()).Compare( x1, y1 ));
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif        void Swap(ref object x, ref object y ) https://blog.51cto.com/iteyer/Images/dot.gif{
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            object temp = null;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            temp = x;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            x = y;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            y = temp;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedBlockEnd.gif    }


测试代码
UserClass

https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedBlock.gifpublic       class    UserClass    https://blog.51cto.com/iteyer/Images/dot.gif   {
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif    protected string _name;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif    protected int _age;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif    protected string _address;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif    public UserClass(string _name, int _age, string _address) https://blog.51cto.com/iteyer/Images/dot.gif{
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif        this._name = _name;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif        this._age = _age;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif        this._address = _address;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif    public string Name https://blog.51cto.com/iteyer/Images/dot.gif{
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif        get https://blog.51cto.com/iteyer/Images/dot.gif{ return _name; }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif        set https://blog.51cto.com/iteyer/Images/dot.gif{ _name = value; }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif    public int Age https://blog.51cto.com/iteyer/Images/dot.gif{
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif        get https://blog.51cto.com/iteyer/Images/dot.gif{ return _age; }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif        set https://blog.51cto.com/iteyer/Images/dot.gif{ _age = value; }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif    public string Address https://blog.51cto.com/iteyer/Images/dot.gif{
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif        get https://blog.51cto.com/iteyer/Images/dot.gif{ return _address; }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedSubBlock.gif        set https://blog.51cto.com/iteyer/Images/dot.gif{ _address = value; }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedBlockEnd.gif}

main
其中ReverserClass reverser =   new  ReverserClass(user.GetType(),  " Name " ); 中的要传递ArrayList数组中的对象的类型和需要作为排序依据的字段名称!

https://blog.51cto.com/iteyer/Images/OutliningIndicators/None.gif           static       void    Main(   string   [] args)
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedBlockStart.gifhttps://blog.51cto.com/iteyer/Images/OutliningIndicators/ContractedBlock.gif           https://blog.51cto.com/iteyer/Images/dot.gif   {
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            UserClass user;
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            ArrayList ar = new ArrayList();
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            user = new UserClass("1", 1, "1");
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            ar.Add(user);
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            user = new UserClass("9", 9, "9");
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            ar.Add(user);
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            user = new UserClass("4", 4, "4");
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            ar.Add(user);
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            user = new UserClass("3", 3, "3");
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            ar.Add(user);
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            user = new UserClass("6", 6, "6");
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            ar.Add(user);
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            Console.WriteLine(https://blog.51cto.com/iteyer/Images/dot.gif);
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            ReverserClass reverser = new ReverserClass(user.GetType(), "Name", "DESC");
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            ar.Sort(reverser);
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif            Console.WriteLine(https://blog.51cto.com/iteyer/Images/dot.gif);
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif
https://blog.51cto.com/iteyer/Images/OutliningIndicators/InBlock.gif
https://blog.51cto.com/iteyer/Images/OutliningIndicators/ExpandedBlockEnd.gif        }


文档来源:51CTO技术博客https://blog.51cto.com/iteyer/3238392
页: [1]
查看完整版本: 任意对象数组ArrayList的排序法(可自定义排序字段、排序方向)