Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Sort Method (IComparer(T))

List(T).Sort Method (IComparer(T))

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

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

member Sort : 
        comparer:IComparer<'T> -> unit 

Parameters

comparer
Type: System.Collections.Generic.IComparer(T)
The IComparer(T) implementation to use when comparing elements, or a null reference (Nothing in Visual Basic) to use the default comparer Comparer(T).Default.

ExceptionCondition
InvalidOperationException

comparer is a null reference (Nothing in Visual Basic), and the default comparer Comparer(T).Default cannot find implementation of the IComparable(T) generic interface or the IComparable interface for type T.

ArgumentException

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

If comparer is provided, the elements of the List(T) are sorted using the specified IComparer(T) implementation.

If comparer is a null reference (Nothing in Visual Basic), 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 uses the 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 example demonstrates the Sort(IComparer(T)) method overload and the BinarySearch(T, IComparer(T)) method overload.

The code 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 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 is displayed, sorted using the alternate comparer, and displayed again.

The BinarySearch(T, IComparer(T)) method overload is then used to search for several strings that are not in the list, employing the alternate comparer. The Insert method is used to insert the strings. These two methods are located in the function named SearchAndInsert, along with code to take the bitwise complement (the ~ operator in C# and Visual C++, Xor -1 in Visual Basic) of the negative number returned by BinarySearch(T, IComparer(T)) and use it as an index for inserting the new string.

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

.NET Framework

Supported in: 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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2015 Microsoft