ArrayList.Sort Method (Int32, Int32, IComparer)
Sorts the elements in a range of elements in ArrayList using the specified comparer.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Parameters
- index
- Type: System.Int32
The zero-based starting index of the range to sort.
- count
- Type: System.Int32
The length of the range to sort.
- comparer
- Type: System.Collections.IComparer
The IComparer implementation to use when comparing elements.
-or-
A null reference (Nothing in Visual Basic) to use the IComparable implementation of each element.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | index is less than zero. -or- count is less than zero. |
| ArgumentException | index and count do not specify a valid range in the ArrayList. |
| NotSupportedException | The ArrayList is read-only. |
| InvalidOperationException | An error occurred while comparing two elements. |
If comparer is set to null, this method performs a comparison sort (also called 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. To perform a stable sort, you must implement a custom IComparer interface.
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 System; using System.Collections; public class SamplesArrayList { public class myReverserClass : IComparer { // Calls CaseInsensitiveComparer.Compare with the parameters reversed. int IComparer.Compare( Object x, Object y ) { return( (new CaseInsensitiveComparer()).Compare( y, x ) ); } } public static void Main() { // 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 ); } public static void PrintIndexAndValues( IEnumerable myList ) { int i = 0; foreach ( Object obj in myList ) 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 */
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.