List<'T>.Sort Method ()


Sorts the elements in the entire List<'T> using the default comparer.

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

member Sort : unit -> unit

Exception Condition

The default comparer Comparer<'T>.Default cannot find an implementation of the IComparable<'T> generic interface or the IComparable interface for type T.

This method uses the default comparer Comparer<'T>.Default for type T to determine the order of list elements. The Comparer<'T>.Default property checks whether type T implements the IComparable<'T> generic interface and uses that implementation, if available. If not, Comparer<'T>.Default checks whether type T implements the IComparable interface. If type T does not implement either interface, Comparer<'T>.Default throws an InvalidOperationException.

This method uses the Array.Sort method, 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 example adds some names to a List<String> object, displays the list in unsorted order, calls the Sort method, and then displays the sorted list.

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

The following code demonstrates the Sort() and Sort(Comparison<'T>) 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(Comparison<'T>) method is implemented by using an anonymous method.

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

The following example demonstrates the Sort() method overload and the BinarySearch('T) method overload. A List<'T> of strings is created and populated with four strings, in no particular order. The list is displayed, sorted, and displayed again.

The BinarySearch('T) method overload is then used to search for two strings that are not in the list, and the Insert method is used to insert them. The return value of the BinarySearch method is negative in each case, because the strings are not in the list. Taking the bitwise complement (the ~ operator in C# and Visual C++, Xor -1 in Visual Basic) of this negative number produces the index of the first element in the list that is larger than the search string, and inserting at this location preserves the sort order. The second search string is larger than any element in the list, so the insertion position is at the end of the list.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top