Sort List C# LINQ

public: void Sort[System::Collections::Generic::IComparer ^ comparer]; public void Sort [System.Collections.Generic.IComparer comparer]; public void Sort [System.Collections.Generic.IComparer? comparer]; member this.Sort : System.Collections.Generic.IComparerLength.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 SearchAndInsert[List^ list, String^ insert, DinoComparer^ dc] { Console::WriteLine["\nBinarySearch and Insert \"{0}\":", insert]; int index = list->BinarySearch[insert, dc]; if [index < 0] { list->Insert[~index, insert]; } }; void Display[List^ list] { Console::WriteLine[]; for each[String^ s in list] { Console::WriteLine[s]; } }; void main[] { List^ dinosaurs = gcnew List[]; dinosaurs->Add["Pachycephalosaurus"]; dinosaurs->Add["Amargasaurus"]; dinosaurs->Add["Mamenchisaurus"]; dinosaurs->Add["Deinonychus"]; Display[dinosaurs]; DinoComparer^ dc = gcnew DinoComparer[]; Console::WriteLine["\nSort with alternate comparer:"]; dinosaurs->Sort[dc]; Display[dinosaurs]; SearchAndInsert[dinosaurs, "Coelophysis", dc]; Display[dinosaurs]; SearchAndInsert[dinosaurs, "Oviraptor", dc]; Display[dinosaurs]; SearchAndInsert[dinosaurs, "Tyrannosaur", dc]; Display[dinosaurs]; SearchAndInsert[dinosaurs, nullptr, dc]; Display[dinosaurs]; } /* This code example produces the following output: Pachycephalosaurus Amargasaurus Mamenchisaurus Deinonychus Sort with alternate comparer: Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Coelophysis": Coelophysis Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Oviraptor": Oviraptor Coelophysis Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Tyrannosaur": Oviraptor Coelophysis Deinonychus Tyrannosaur Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "": Oviraptor Coelophysis Deinonychus Tyrannosaur Amargasaurus Mamenchisaurus Pachycephalosaurus */ using System; using System.Collections.Generic; public class DinoComparer: IComparer { 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 dinosaurs = new List[]; dinosaurs.Add["Pachycephalosaurus"]; dinosaurs.Add["Amargasaurus"]; dinosaurs.Add["Mamenchisaurus"]; dinosaurs.Add["Deinonychus"]; Display[dinosaurs]; DinoComparer dc = new DinoComparer[]; Console.WriteLine["\nSort with alternate comparer:"]; dinosaurs.Sort[dc]; Display[dinosaurs]; SearchAndInsert[dinosaurs, "Coelophysis", dc]; Display[dinosaurs]; SearchAndInsert[dinosaurs, "Oviraptor", dc]; Display[dinosaurs]; SearchAndInsert[dinosaurs, "Tyrannosaur", dc]; Display[dinosaurs]; SearchAndInsert[dinosaurs, null, dc]; Display[dinosaurs]; } private static void SearchAndInsert[List list, string insert, DinoComparer dc] { Console.WriteLine["\nBinarySearch and Insert \"{0}\":", insert]; int index = list.BinarySearch[insert, dc]; if [index < 0] { list.Insert[~index, insert]; } } private static void Display[List list] { Console.WriteLine[]; foreach[ string s in list ] { Console.WriteLine[s]; } } } /* This code example produces the following output: Pachycephalosaurus Amargasaurus Mamenchisaurus Deinonychus Sort with alternate comparer: Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Coelophysis": Coelophysis Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Oviraptor": Oviraptor Coelophysis Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Tyrannosaur": Oviraptor Coelophysis Deinonychus Tyrannosaur Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "": Oviraptor Coelophysis Deinonychus Tyrannosaur Amargasaurus Mamenchisaurus Pachycephalosaurus */ Imports System.Collections.Generic Public Class DinoComparer Implements IComparer[Of String] Public Function Compare[ByVal x As String, _ ByVal y As String] As Integer _ Implements IComparer[Of String].Compare If x Is Nothing Then If y Is Nothing Then ' If x is Nothing and y is Nothing, they're ' equal. Return 0 Else ' If x is Nothing and y is not Nothing, y ' is greater. Return -1 End If Else ' If x is not Nothing... ' If y Is Nothing Then ' ...and y is Nothing, x is greater. Return 1 Else ' ...and y is not Nothing, compare the ' lengths of the two strings. ' Dim retval As Integer = _ x.Length.CompareTo[y.Length] If retval 0 Then ' 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] End If End If End If End Function End Class Public Class Example Public Shared Sub Main[] Dim dinosaurs As New List[Of String] dinosaurs.Add["Pachycephalosaurus"] dinosaurs.Add["Amargasaurus"] dinosaurs.Add["Mamenchisaurus"] dinosaurs.Add["Deinonychus"] Display[dinosaurs] Dim dc As New DinoComparer Console.WriteLine[vbLf & "Sort with alternate comparer:"] dinosaurs.Sort[dc] Display[dinosaurs] SearchAndInsert[dinosaurs, "Coelophysis", dc] Display[dinosaurs] SearchAndInsert[dinosaurs, "Oviraptor", dc] Display[dinosaurs] SearchAndInsert[dinosaurs, "Tyrannosaur", dc] Display[dinosaurs] SearchAndInsert[dinosaurs, Nothing, dc] Display[dinosaurs] End Sub Private Shared Sub SearchAndInsert[ _ ByVal lis As List[Of String], _ ByVal insert As String, ByVal dc As DinoComparer] Console.WriteLine[vbLf & _ "BinarySearch and Insert ""{0}"":", insert] Dim index As Integer = lis.BinarySearch[insert, dc] If index < 0 Then index = index Xor -1 lis.Insert[index, insert] End If End Sub Private Shared Sub Display[ByVal lis As List[Of String]] Console.WriteLine[] For Each s As String In lis Console.WriteLine[s] Next End Sub End Class ' This code example produces the following output: ' 'Pachycephalosaurus 'Amargasaurus 'Mamenchisaurus 'Deinonychus ' 'Sort with alternate comparer: ' 'Deinonychus 'Amargasaurus 'Mamenchisaurus 'Pachycephalosaurus ' 'BinarySearch and Insert "Coelophysis": ' 'Coelophysis 'Deinonychus 'Amargasaurus 'Mamenchisaurus 'Pachycephalosaurus ' 'BinarySearch and Insert "Oviraptor": ' 'Oviraptor 'Coelophysis 'Deinonychus 'Amargasaurus 'Mamenchisaurus 'Pachycephalosaurus ' 'BinarySearch and Insert "Tyrannosaur": ' 'Oviraptor 'Coelophysis 'Deinonychus 'Tyrannosaur 'Amargasaurus 'Mamenchisaurus 'Pachycephalosaurus ' 'BinarySearch and Insert "": ' ' 'Oviraptor 'Coelophysis 'Deinonychus 'Tyrannosaur 'Amargasaurus 'Mamenchisaurus 'Pachycephalosaurus

Remarks

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

If comparer is null, 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 the Array.Sort method, which applies the introspective sort as follows:

  • If the partition size is less than or equal to 16 elements, it uses an insertion sort algorithm.

  • If the number of partitions exceeds 2 log n, 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.

This method is an O[n log n] operation, where n is Count.

See also

  • Performing Culture-Insensitive String Operations in Collections

Applies to

Video liên quan

Chủ Đề