List<'T>.Sort Method (Int32, Int32, IComparer<'T>)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Sorts the elements in a range of elements in List<'T> using the specified comparer.

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

member Sort : 
        index:int *
        count:int *
        comparer:IComparer<'T> -> unit


Type: System.Int32

The zero-based starting index of the range to sort.

Type: System.Int32

The length of the range to sort.

Type: System.Collections.Generic.IComparer<'T>

The IComparer<'T> implementation to use when comparing elements, or null to use the default comparer Comparer<'T>.Default.

Exception Condition

index is less than 0.


count is less than 0.


index and count do not specify a valid range in the List<'T>.


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


comparer is null, and the default comparer Comparer<'T>.Default cannot find implementation of the IComparable<'T> generic interface or the IComparable interface for type T.

If comparer is provided, the elements of the List<'T> are sorted using the specified IComparer<'T> implementation.

If comparer is null, the default comparer Comparer<'T>.Default 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 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 example demonstrates the Sort(Int32, Int32, IComparer<'T>) method overload and the BinarySearch(Int32, Int32, 'T, IComparer<'T>) method overload.

The example defines an alternative comparer for strings named DinoCompare, which implements the IComparer<string> (IComparer(Of String) in Visual Basic, IComparer<String^> in Visual C++) generic interface. The comparer works as follows: First, the comparands are tested for null, 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 the names of five herbivorous dinosaurs and three carnivorous dinosaurs. Within each of the two groups, the names are not in any particular sort order. The list is displayed, the range of herbivores is sorted using the alternate comparer, and the list is displayed again.

The BinarySearch(Int32, Int32, 'T, IComparer<'T>) method overload is then used to search only the range of herbivores for "Brachiosaurus". The string is not found, and the bitwise complement (the ~ operator in C# and Visual C++, Xor -1 in Visual Basic) of the negative number returned by the BinarySearch(Int32, Int32, 'T, IComparer<'T>) method is used as an index for inserting the new string.

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