C# ArrayList Sort(Int32, Int32, IComparer)
Description
ArrayList Sort(Int32, Int32, IComparer)
sorts the elements
in a range of elements in ArrayList using the specified comparer.
Syntax
ArrayList.Sort(Int32, Int32, IComparer)
has the following syntax.
public virtual void Sort(
int index,// w w w . j a va 2s . co m
int count,
IComparer comparer
)
Parameters
ArrayList.Sort(Int32, Int32, IComparer)
has the following parameters.
index
- The zero-based starting index of the range to sort.count
- The length of the range to sort.comparer
- The IComparer implementation to use when comparing elements.comparer
- -or-comparer
- A null reference (Nothing in Visual Basic) to use the IComparable implementation of each element.
Returns
ArrayList.Sort(Int32, Int32, IComparer)
method returns
Example
using System;//from w ww .j ava 2 s. c om
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" );
IComparer myComparer = new myReverserClass();
myAL.Sort( 1, 3, myComparer );
foreach ( Object obj in myAL )
Console.WriteLine(obj );
}
}
The code above generates the following result.