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