List<T>::Sort Method (Comparison<T>^)
Sorts the elements in the entire List<T> using the specified System::Comparison<T>.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- comparison
-
Type:
System::Comparison<T>^
The System::Comparison<T> to use when comparing elements.
| Exception | Condition |
|---|---|
| ArgumentNullException | 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 comparison is provided, the elements of the List<T> are sorted using the method represented by the delegate.
If comparison is null, an ArgumentNullException is thrown.
This method uses Array::Sort, which applies the introspective sort as follows:
If the partition size is fewer than 16 elements, it uses an insertion sort algorithm
If the number of partitions exceeds 2 * LogN, where N is the range of the input array, it uses a Heapsort algorithm.
Otherwise, it uses a 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 example demonstrates the Sort(Comparison<T>^) method overload.
The 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 List<T> 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.
using namespace System; using namespace System::Collections::Generic; int CompareDinosByLength(String^ x, String^ y) { if (x == nullptr) { if (y == nullptr) { // 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 == nullptr) // ...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); } } } }; void Display(List<String^>^ list) { Console::WriteLine(); for each(String^ s in list) { if (s == nullptr) Console::WriteLine("(null)"); else Console::WriteLine("\"{0}\"", s); } }; void main() { List<String^>^ dinosaurs = gcnew List<String^>(); dinosaurs->Add("Pachycephalosaurus"); dinosaurs->Add("Amargasaurus"); dinosaurs->Add(""); dinosaurs->Add(nullptr); dinosaurs->Add("Mamenchisaurus"); dinosaurs->Add("Deinonychus"); Display(dinosaurs); Console::WriteLine("\nSort with generic Comparison<String^> delegate:"); dinosaurs->Sort( gcnew Comparison<String^>(CompareDinosByLength)); Display(dinosaurs); } /* This code example produces the following output: "Pachycephalosaurus" "Amargasaurus" "" (null) "Mamenchisaurus" "Deinonychus" Sort with generic Comparison<String^> delegate: (null) "" "Deinonychus" "Amargasaurus" "Mamenchisaurus" "Pachycephalosaurus" */
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1