List<T>.Sort Method
Sorts the elements in the List<T> or a portion of it.
Assembly: mscorlib (in mscorlib.dll)
| Name | Description | |
|---|---|---|
![]() | Sort() | Sorts the elements in the entire List<T> using the default comparer. |
![]() | Sort(Comparison<T>) | Sorts the elements in the entire List<T> using the specified System.Comparison<T>. |
![]() | Sort(IComparer<T>) | Sorts the elements in the entire List<T> using the specified comparer. |
![]() | Sort(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 | The delegate method to use when comparing elements | |
comparer | 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 | The zero-based starting index of the range to sort. | |
count | The length of the range to sort. |
Exception | Condition |
|---|---|
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. | |
comparison is null. | |
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>. | |
index is less than 0 or count is less than 0. |
