List.Sort Method (Int32, Int32, Generic IComparer)
Assembly: mscorlib (in mscorlib.dll)
public void Sort ( int index, int count, IComparer<T> comparer )
public function Sort ( index : int, count : int, comparer : IComparer<T> )
Not applicable.
Parameters
- index
The zero-based starting index of the range to sort.
- count
The length of the range to sort.
- comparer
The IComparer implementation to use when comparing elements, or a null reference (Nothing in Visual Basic) to use the default comparer Comparer.Default.
| Exception type | Condition |
|---|---|
| index is less than 0. -or- count is less than 0. | |
| index and count do not specify a valid range in the List. -or- The implementation of comparer caused an error during the sort. For example, comparer might not return 0 when comparing an item with itself. | |
| comparer is a null reference (Nothing in Visual Basic), and the default comparer Comparer.Default cannot find implementation of the IComparable generic interface or the IComparable interface for type T. |
If comparer is provided, the elements of the List are sorted using the specified IComparer implementation.
If comparer is a null reference (Nothing in Visual Basic), the default comparer Comparer.Default checks whether type T implements the IComparable generic interface and uses that implementation, if available. If not, Comparer.Default checks whether type T implements the IComparable interface. If type T does not implement either interface, Comparer.Default throws an InvalidOperationException.
This method uses System.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(Int32,Int32,Generic IComparer) method overload and the BinarySearch(Int32,Int32,T,Generic IComparer) 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 of strings is created and populated with the names of five herbivorous dinosaurs and three carnivorous dinosaurs. Within each of the two groups, the names are not in any particular sort order. The list is displayed, the range of herbivores is sorted using the alternate comparer, and the list is displayed again.
The BinarySearch(Int32,Int32,T,Generic IComparer) method overload is then used to search only the range of herbivores for "Brachiosaurus". The string is not found, and the bitwise complement (the ~ operator in C# and Visual C++, Xor -1 in Visual Basic) of the negative number returned by the BinarySearch(Int32,Int32,T,Generic IComparer) method is used as an index for inserting the new string.
using System; using System.Collections.Generic; public class DinoComparer: IComparer<string> { public int Compare(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 class Example { public static void Main() { List<string> dinosaurs = new List<string>(); dinosaurs.Add("Pachycephalosaurus"); dinosaurs.Add("Parasauralophus"); dinosaurs.Add("Amargasaurus"); dinosaurs.Add("Galimimus"); dinosaurs.Add("Mamenchisaurus"); dinosaurs.Add("Deinonychus"); dinosaurs.Add("Oviraptor"); dinosaurs.Add("Tyrannosaurus"); int herbivores = 5; Display(dinosaurs); DinoComparer dc = new DinoComparer(); Console.WriteLine("\nSort a range with the alternate comparer:"); dinosaurs.Sort(0, herbivores, dc); Display(dinosaurs); Console.WriteLine("\nBinarySearch a range and Insert \"{0}\":", "Brachiosaurus"); int index = dinosaurs.BinarySearch(0, herbivores, "Brachiosaurus", dc); if (index < 0) { dinosaurs.Insert(~index, "Brachiosaurus"); herbivores++; } Display(dinosaurs); } private static void Display(List<string> list) { Console.WriteLine(); foreach( string s in list ) { Console.WriteLine(s); } } } /* This code example produces the following output: Pachycephalosaurus Parasauralophus Amargasaurus Galimimus Mamenchisaurus Deinonychus Oviraptor Tyrannosaurus Sort a range with the alternate comparer: Galimimus Amargasaurus Mamenchisaurus Parasauralophus Pachycephalosaurus Deinonychus Oviraptor Tyrannosaurus BinarySearch a range and Insert "Brachiosaurus": Galimimus Amargasaurus Brachiosaurus Mamenchisaurus Parasauralophus Pachycephalosaurus Deinonychus Oviraptor Tyrannosaurus */
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.