Sort the elements in a range of elements in ArrayList using the specified comparer in CSharp
Description
The following code shows how to sort the elements in a range of elements in ArrayList using the specified comparer.
Example
using System;/*from w ww.ja va2s . com*/
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" );
myAL.Add( "1" );
myAL.Add( "2" );
myAL.Add( "3" );
myAL.Add( "X" );
myAL.Add( "Y" );
myAL.Add( "Z" );
foreach ( Object obj in myAL )
Console.WriteLine(obj );
IComparer myComparer = new myReverserClass();
myAL.Sort( 1, 3, myComparer );
foreach ( Object obj in myAL )
Console.WriteLine(obj );
}
}
The code above generates the following result.