SortCollectionBase

 public abstract class SortCollectionBase:CollectionBase
 {
  class ReverserComparer : IComparer 
  {
   Type type = null;
   string name = string.Empty;
   string direction = "ASC";
   public ReverserComparer(Type type, string name, string direction)
   {
    this.type = type;
    this.name = name;
    direction = direction.ToUpper();

    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;

  public void Sort( string name )
  {
   this.Sort( name ,"asc");
  }
  /// <summary>
  ///
  /// </summary>
  /// <param name="name"></param>
  /// <param name="direction">eg:asc or desc</param>
  public void Sort( string name ,string direction)
  {
   if( this.List.Count == 0 )
    return ;
   if( _type == null )
    _type = this.List[0].GetType();
   ReverserComparer rc = new ReverserComparer( _type ,name ,direction );
   this.InnerList.Sort( rc );
  }

posted on 2005-01-13 17:11  Kain  阅读(327)  评论(0)    收藏  举报

导航