Export (0) Print
Expand All

List(T).Sort Method (Comparison(T))

Sorts the elements in the entire List(T) using the specified System.Comparison(T).

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

member Sort : 
        comparison:Comparison<'T> -> unit

Parameters

comparison
Type: System.Comparison(T)

The System.Comparison(T) to use when comparing elements.

ExceptionCondition
ArgumentNullException

comparison is a null reference (Nothing in Visual Basic).

ArgumentException

The implementation of comparison caused an error during the sort. For example, comparison might not return 0 when comparing an item with itself.

If comparison is provided, the elements of the List(T) are sorted using the method represented by the delegate.

If comparison is a null reference (Nothing in Visual Basic), an ArgumentNullException is thrown.

This method uses Array.Sort, , which applies the introspective sort as follows:

  • If the partition size is fewer than 16 elements, it uses an insertion sort algorithm

  • If the number of partitions exceeds 2 * LogN, where N is the range of the input array, it uses a Heapsort algorithm.

  • Otherwise, it uses a 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 demonstrates the Sort and Sort method overloads on a simple business object. Calling the Sort method results in the use of the default comparer for the Part type, and the Sort method is implemented using an anonymous method.

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

The following example demonstrates the Sort(Comparison(T)) method overload.

The example defines an alternative comparison method for strings, named CompareDinosByLength. This method works as follows: First, the comparands are tested for a null reference (Nothing in Visual Basic), and a null reference is treated as less than a non-null. Second, the string lengths are compared, and the longer string is deemed to be greater. Third, if the lengths are equal, ordinary string comparison is used.

A List(T) of strings is created and populated with four strings, in no particular order. The list also includes an empty string and a null reference. The list is displayed, sorted using a Comparison(T) generic delegate representing the CompareDinosByLength method, and displayed again.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

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.

Show:
© 2014 Microsoft