IComparer Interface Exposes a method that compares two objects. : IComparer « Collections Data Structure « C# / C Sharp






IComparer Interface Exposes a method that compares two objects.

  
using System;
using System.Collections;

public class SamplesArrayList  {
   public class myReverserClass : IComparer  {
      int IComparer.Compare( Object x, Object y )  {
          return( (new CaseInsensitiveComparer()).Compare( y, x ) );
      }
   }
   public static void Main()  {
      ArrayList myAL = new ArrayList();
      myAL.Add( "A" );
      myAL.Add( "B" );
      myAL.Add( "C" );
      PrintIndexAndValues( myAL );
      myAL.Sort();
      PrintIndexAndValues( myAL );

      IComparer myComparer = new myReverserClass();
      myAL.Sort( myComparer );
      PrintIndexAndValues( myAL );
   }
   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      foreach ( Object obj in myList )
         Console.WriteLine( "\t[{0}]:\t{1}", i++, obj );
   }
}

   
    
  








Related examples in the same category

1.Comparer Class Compares two objects for equivalence, where string comparisons are case-sensitive.
2.EqualityComparer(T) Class provides a base class for implementations of the IEqualityComparer generic interface.
3.Compares the two values passed in and checks if they're value-wise the same.