ArrayList.Sort Method (Int32, Int32, IComparer)
Assembly: mscorlib (in mscorlib.dll)
public void Sort ( int index, int count, IComparer comparer )
public function Sort ( index : int, count : int, comparer : IComparer )
Not applicable.
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.
-or-
a null reference (Nothing in Visual Basic) to use the IComparable implementation of each element.
This method uses Array.Sort, which uses the QuickSort algorithm. This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.
On average, this method is an O(n log n) operation, where n is count; in the worst case it is an O(n^2) operation.
The following code example shows how to sort the values in a range of elements in an ArrayList using the default comparer and a custom comparer that reverses the sort order.
using namespace System; using namespace System::Collections; void PrintIndexAndValues( IEnumerable^ myList ); ref class myReverserClass: public IComparer { private: // Calls CaseInsensitiveComparer.Compare with the parameters reversed. virtual int Compare( Object^ x, Object^ y ) = IComparer::Compare { return ((gcnew CaseInsensitiveComparer)->Compare( y, x )); } }; int main() { // Creates and initializes a new ArrayList. ArrayList^ myAL = gcnew ArrayList; myAL->Add( "The" ); myAL->Add( "QUICK" ); myAL->Add( "BROWN" ); myAL->Add( "FOX" ); myAL->Add( "jumped" ); myAL->Add( "over" ); myAL->Add( "the" ); myAL->Add( "lazy" ); myAL->Add( "dog" ); // Displays the values of the ArrayList. Console::WriteLine( "The ArrayList initially contains the following values:" ); PrintIndexAndValues( myAL ); // Sorts the values of the ArrayList using the default comparer. myAL->Sort( 1, 3, nullptr ); Console::WriteLine( "After sorting from index 1 to index 3 with the default comparer:" ); PrintIndexAndValues( myAL ); // Sorts the values of the ArrayList using the reverse case-insensitive comparer. IComparer^ myComparer = gcnew myReverserClass; myAL->Sort( 1, 3, myComparer ); Console::WriteLine( "After sorting from index 1 to index 3 with the reverse case-insensitive comparer:" ); PrintIndexAndValues( myAL ); } void PrintIndexAndValues( IEnumerable^ myList ) { int i = 0; IEnumerator^ myEnum = myList->GetEnumerator(); while ( myEnum->MoveNext() ) { Object^ obj = safe_cast<Object^>(myEnum->Current); Console::WriteLine( "\t[{0}]:\t{1}", i++, obj ); } Console::WriteLine(); } /* This code produces the following output. The ArrayList initially contains the following values: [0]: The [1]: QUICK [2]: BROWN [3]: FOX [4]: jumped [5]: over [6]: the [7]: lazy [8]: dog After sorting from index 1 to index 3 with the default comparer: [0]: The [1]: BROWN [2]: FOX [3]: QUICK [4]: jumped [5]: over [6]: the [7]: lazy [8]: dog After sorting from index 1 to index 3 with the reverse case-insensitive comparer: [0]: The [1]: QUICK [2]: FOX [3]: BROWN [4]: jumped [5]: over [6]: the [7]: lazy [8]: dog */
import System.*;
import System.Collections.*;
public class SamplesArrayList
{
public static class MyReverserClass implements IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
public int Compare(Object x, Object y)
{
return(new CaseInsensitiveComparer()).Compare(y, x) ;
} //IComparer.Compare
} //MyReverserClass
public static void main(String[] args)
{
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("The");
myAL.Add("QUICK");
myAL.Add("BROWN");
myAL.Add("FOX");
myAL.Add("jumped");
myAL.Add("over");
myAL.Add("the");
myAL.Add("lazy");
myAL.Add("dog");
// Displays the values of the ArrayList.
Console.WriteLine("The ArrayList initially contains the following"
+ " values:");
PrintIndexAndValues(myAL);
// Sorts the values of the ArrayList using the default comparer.
myAL.Sort(1, 3, null);
Console.WriteLine("After sorting from index 1 to index 3 with the"
+ " default comparer:");
PrintIndexAndValues(myAL);
// Sorts the values of the ArrayList using the
// reverse case-insensitive comparer.
IComparer myComparer = new MyReverserClass();
myAL.Sort(1, 3, myComparer);
Console.WriteLine("After sorting from index 1 to index 3 with the"
+ " reverse case-insensitive comparer:");
PrintIndexAndValues(myAL);
} //main
public static void PrintIndexAndValues(IEnumerable myList)
{
int i = 0;
IEnumerator objEnum = myList.GetEnumerator();
while(objEnum.MoveNext()) {
Object obj= (IEnumerable)objEnum.get_Current();
Console.WriteLine("\t[{0}]:\t{1}", (Int32)i,obj);
i++;
}
Console.WriteLine();
} //PrintIndexAndValues
} //SamplesArrayList
/*
This code produces the following output.
The ArrayList initially contains the following values:
[0]: The
[1]: QUICK
[2]: BROWN
[3]: FOX
[4]: jumped
[5]: over
[6]: the
[7]: lazy
[8]: dog
After sorting from index 1 to index 3 with the default comparer:
[0]: The
[1]: BROWN
[2]: FOX
[3]: QUICK
[4]: jumped
[5]: over
[6]: the
[7]: lazy
[8]: dog
After sorting from index 1 to index 3 with the reverse case-insensitive
comparer:
[0]: The
[1]: QUICK
[2]: FOX
[3]: BROWN
[4]: jumped
[5]: over
[6]: the
[7]: lazy
[8]: dog
*/
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.