Topic: Building a Comparer Class for a Generic List<>
Share/Save/Bookmark
[Serailiazable()]
public class MyClass
{
   public enum SortDirection
   {
      ASC = 0,
      DESC = 1
   }
   public class Comparer
   {
      public class MyClassSomeField : System.Collections.Generic.IComparer<MyClass>
      {
         private MyClass.SortDirection _SortDirection;
         
         public void Comparer( MyClass.SortDirection SortDirection )
         {
            _SortDirection = SortDirection;
         }
         int System.Collections.Generic.IComparer<MyClass>.Compare( MyClass Obj1, MyClass Obj2 )
         {
            switch( _SortDirection )
            {
               case SortDirection.ASC:
                  {
                     return (int)Obj1.MyClassSomeField.ToLower().CompareTo( Obj2.MyClassSomeField.ToLower() );
                  }
               case SortDirection.DESC:
                  {
                     return (int)Obj2.MyClassSomeField.ToLower().CompareTo( Obj1.MyClassSomeField.ToLower() );
                  }
               default:
                     return 0;
            }
         }
      }
   }
}