Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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)

abstract Sort : 
        index:int * 
        count:int * 
        comparer:IComparer -> unit  
override Sort : 
        index:int * 
        count:int * 
        comparer:IComparer -> unit

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.

ExceptionCondition
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 a null reference (Nothing in Visual Basic), 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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft