ArrayList 如何通过IComparable接口 实现Sort

解决方案 »

  1.   


    using System;
    using System.Collections;
    using System.Reflection;namespace Seine.Jollo3.Utility.Share
    {
        public class ArrayListSort : IComparer  
        { 
            Type   _type      = null; 
            string _name      = string.Empty; 
            string _direction = "ASC";         public ArrayListSort(Type type, string name, string direction) 
            { 
                this._type      = type; 
                this._name      = name; 
                this._direction = direction.ToUpper().Equals("DESC") ? "DESC" : "ASC";
            } 
     
            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; 
            }  
        } 
    }