Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Array.Sort(T) Method (T[])

Sorts the elements in an entire Array using the IComparable(T) generic interface implementation of each element of the Array.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
JScript does not support generic types or methods.

Type Parameters

T

The type of the elements of the array.

Parameters

array
Type: T[]

The one-dimensional, zero-based Array to sort.

ExceptionCondition
ArgumentNullException

array is a null reference (Nothing in Visual Basic).

InvalidOperationException

One or more elements in array do not implement the IComparable(T) generic interface.

Each element of array must implement the IComparable(T) generic interface to be capable of comparisons with every other element in array.

If the sort is not successfully completed, the results are undefined.

This method 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 the Length of array; in the worst case it is an O(n ^ 2) operation.

The following code example demonstrates the Sort(T)(T[]) generic method overload and the BinarySearch(T)(T[], T) generic method overload. An array of strings is created, in no particular order.

The array is displayed, sorted, and displayed again.

NoteNote:

The calls to the Sort and BinarySearch generic methods do not look any different from calls to their nongeneric counterparts, because Visual Basic, C#, and C++ infer the type of the generic type parameter from the type of the first argument. If you use the MSIL Disassembler (Ildasm.exe) to examine the Microsoft intermediate language (MSIL), you can see that the generic methods are being called.

The BinarySearch(T)(T[], T) generic method overload is then used to search for two strings, one that is not in the array and one that is. The array and the return value of the BinarySearch method are passed to the ShowWhere generic method, which displays the index value if the string is found, and otherwise the elements the search string would fall between if it were in the array. The index is negative if the string is not n the array, so the ShowWhere method takes the bitwise complement (the ~ operator in C# and Visual C++, Xor -1 in Visual Basic) to obtain the index of the first element in the list that is larger than the search string.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.