Array.Sort<T> Method (T[], Comparison<T>)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Sorts the elements in an Array using the specified Comparison<T>.
Assembly: mscorlib (in mscorlib.dll)
Type Parameters
- T
The type of the elements of the array.
Parameters
- array
- Type:
T
[]
The one-dimensional, zero-based Array to sort
- comparison
- Type: System.Comparison<T>
The Comparison<T> to use when comparing elements.
| Exception | Condition |
|---|---|
| ArgumentNullException | array is null. -or- comparison is null. |
| ArgumentException | The implementation of comparison caused an error during the sort. For example, comparison might not return 0 when comparing an item with itself. |
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(Comparison<T>) method overload.
The code example defines an alternative comparison method for strings, named CompareDinosByLength. This method 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 array of strings is created and populated with four strings, in no particular order. The list also includes an empty string and a null reference. The list is displayed, sorted using a Comparison<T> generic delegate representing the CompareDinosByLength method, and displayed again.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
using System; using System.Collections.Generic; public class Example { private static int CompareDinosByLength(string x, string y) { if (x == null) { if (y == null) { // If x is null and y is null, they're // equal. return 0; } else { // If x is null and y is not null, y // is greater. return -1; } } else { // If x is not null... // if (y == null) // ...and y is null, x is greater. { return 1; } else { // ...and y is not null, compare the // lengths of the two strings. // int retval = x.Length.CompareTo(y.Length); if (retval != 0) { // If the strings are not of equal length, // the longer string is greater. // return retval; } else { // If the strings are of equal length, // sort them with ordinary string comparison. // return x.CompareTo(y); } } } } public static void Demo(System.Windows.Controls.TextBlock outputBlock) { string[] dinosaurs = { "Pachycephalosaurus", "Amargasaurus", "", null, "Mamenchisaurus", "Deinonychus" }; Display(outputBlock, dinosaurs); outputBlock.Text += "\nSort with generic Comparison<string> delegate:" + "\n"; Array.Sort(dinosaurs, CompareDinosByLength); Display(outputBlock, dinosaurs); } private static void Display(System.Windows.Controls.TextBlock outputBlock, string[] arr) { outputBlock.Text += "\n"; foreach (string s in arr) { if (s == null) outputBlock.Text += "(null)" + "\n"; else outputBlock.Text += String.Format("\"{0}\"", s) + "\n"; } } } /* This code example produces the following output: "Pachycephalosaurus" "Amargasaurus" "" (null) "Mamenchisaurus" "Deinonychus" Sort with generic Comparison<string> delegate: (null) "" "Deinonychus" "Amargasaurus" "Mamenchisaurus" "Pachycephalosaurus" */
Note: