ArrayList.Sort Method (IComparer)

 

Sorts the elements in the entire ArrayList using the specified comparer.

Namespace:   System.Collections
Assembly:  mscorlib (in mscorlib.dll)

abstract Sort : 
        comparer:IComparer -> unit
override Sort : 
        comparer:IComparer -> unit

Parameters

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
NotSupportedException

The ArrayList is read-only.

InvalidOperationException

An error occurred while comparing two elements.

ArgumentException

null is passed for comparer, and the elements in the list do not implement IComparable.

Use the Sort method to sort a list of objects with a custom comparer that implements the IComparer interface. If you pass null for comparer, this method uses the IComparable implementation of each element. In this case, you must make sure that the objects contained in the list implement the IComparer interface or an exception will occur.

In addition, using the IComparable implementation means the list 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 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.

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top
Show: