Export (0) Print
Expand All
0 out of 3 rated this helpful - Rate this topic

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.

ExceptionCondition
NotSupportedException

The ArrayList is read-only.

InvalidOperationException

An error occurred while comparing two elements.

ArgumentException

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.