Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

List<T>.Sort Method

 

Sorts the elements in the List<T> or a portion of it.

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

NameDescription
System_CAPS_pubmethodSort()

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

System_CAPS_pubmethodSort(Comparison<T>)

Sorts the elements in the entire List<T> using the specified System.Comparison<T>.

System_CAPS_pubmethodSort(IComparer<T>)

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

System_CAPS_pubmethodSort(Int32, Int32, IComparer<T>)

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

The Sort methods enable you to sort with the default comparer for the object type in the list, or specify a customer sort method.

List<T>.Sort()

Sorts the elements in the entire list using the default comparer of the object type held in the list.

List<T>.Sort M(Comparison<T> comparison)

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

List<T>.Sort(IComparer<T> comparer)

Sorts the elements in the entire list using the specified System.Comparison<T>.

List<T>.Sort(Int32 index, Int32 count, IComparer<T> comparer)

Sorts the elements in a range of elements in list using the specified comparer.

Parameter

Type

Description

comparison

Comparison<T>

The delegate method to use when comparing elements

comparer

IComparer<T>

The HYPERLINK "http://msdn.microsoft.com/en-us/library/8ehhxeaf(v=vs.110).aspx" IComparer<T> implementation to use when comparing elements, or null to use the default comparer HYPERLINK "http://msdn.microsoft.com/en-us/library/azhsac5f(v=vs.110).aspx" Comparer<T>.Default.

index

Int32

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

count

Int32

The length of the range to sort.


Exception

Condition

InvalidOperationException

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

ArgumentNullException

comparison is null.

ArgumentException

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

-or-

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

ArgumentOutOfRangeException

index is less than 0 or count is less than 0.

Return to top
Show:
© 2016 Microsoft