Reference: https://msdn.microsoft.com/zh-tw/library/b0zbh7b6(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.collections.icomparer.aspx ---------- List Sort() using System; using System.Collections.Generic; public class Example { public static void Main() { String[] names = { "Samuel", "Dakota", "Koani", "Saya", "Vanya", "Yiska", "Yuma", "Jody", "Nikita" }; var nameList = new List(); nameList.AddRange(names); Console.WriteLine("List in unsorted order: "); foreach (var name in nameList) Console.Write(" {0}", name); Console.WriteLine(Environment.NewLine); nameList.Sort(); Console.WriteLine("List in sorted order: "); foreach (var name in nameList) Console.Write(" {0}", name); Console.WriteLine(); } } // The example displays the following output: // List in unsorted order: // Samuel Dakota Koani Saya Vanya Yiska Yuma Jody Nikita // // List in sorted order: // Dakota Jody Koani Nikita Samuel Saya Vanya Yiska Yuma ---------- List Sort(Comparison) using System; using System.Collections.Generic; // Simple business object. A PartId is used to identify the type of part // but the part name can change. public class Part : IEquatable , IComparable { public string PartName { get; set; } public int PartId { get; set; } public override string ToString() { return "ID: " + PartId + " Name: " + PartName; } public override bool Equals(object obj) { if (obj == null) return false; Part objAsPart = obj as Part; if (objAsPart == null) return false; else return Equals(objAsPart); } public int SortByNameAscending(string name1, string name2) { return name1.CompareTo(name2); } // Default comparer for Part type. public int CompareTo(Part comparePart) { // A null value means that this object is greater. if (comparePart == null) return 1; else return this.PartId.CompareTo(comparePart.PartId); } public override int GetHashCode() { return PartId; } public bool Equals(Part other) { if (other == null) return false; return (this.PartId.Equals(other.PartId)); } // Should also override == and != operators. } public class Example { public static void Main() { // Create a list of parts. List parts = new List(); // Add parts to the list. parts.Add(new Part() { PartName = "regular seat", PartId = 1434 }); parts.Add(new Part() { PartName= "crank arm", PartId = 1234 }); parts.Add(new Part() { PartName = "shift lever", PartId = 1634 }); ; // Name intentionally left null. parts.Add(new Part() { PartId = 1334 }); parts.Add(new Part() { PartName = "banana seat", PartId = 1444 }); parts.Add(new Part() { PartName = "cassette", PartId = 1534 }); // Write out the parts in the list. This will call the overridden // ToString method in the Part class. Console.WriteLine("\nBefore sort:"); foreach (Part aPart in parts) { Console.WriteLine(aPart); } // Call Sort on the list. This will use the // default comparer, which is the Compare method // implemented on Part. parts.Sort(); Console.WriteLine("\nAfter sort by part number:"); foreach (Part aPart in parts) { Console.WriteLine(aPart); } // This shows calling the Sort(Comparison(T) overload using // an anonymous method for the Comparison delegate. // This method treats null as the lesser of two values. parts.Sort(delegate(Part x, Part y) { if (x.PartName == null && y.PartName == null) return 0; else if (x.PartName == null) return -1; else if (y.PartName == null) return 1; else return x.PartName.CompareTo(y.PartName); }); Console.WriteLine("\nAfter sort by name:"); foreach (Part aPart in parts) { Console.WriteLine(aPart); } /* Before sort: ID: 1434 Name: regular seat ID: 1234 Name: crank arm ID: 1634 Name: shift lever ID: 1334 Name: ID: 1444 Name: banana seat ID: 1534 Name: cassette After sort by part number: ID: 1234 Name: crank arm ID: 1334 Name: ID: 1434 Name: regular seat ID: 1444 Name: banana seat ID: 1534 Name: cassette ID: 1634 Name: shift lever After sort by name: ID: 1334 Name: ID: 1444 Name: banana seat ID: 1534 Name: cassette ID: 1234 Name: crank arm ID: 1434 Name: regular seat ID: 1634 Name: shift lever */ } } ---------- 下列範例會示範 Sort() 方法多載和 BinarySearch(T) 方法多載。 A List 的字串建立並填入任何特定順序排列的四個字串。 清單會顯示已排序,並顯示一次。 BinarySearch(T) 方法多載則用來搜尋兩個字串時,在清單中,而 Insert 方法用來將其插入。 傳回值 BinarySearch 方法是在每個案例中,負數,因為字串不在清單中。 取得位元補數 (~ 運算子在 C# 和 Visual c + + 中, Xor 在 Visual Basic 中的為-1) 的此負的數字會產生此位置是大於搜尋字串,並插入清單中的第一個元素的索引會保留排序順序。 第二個搜尋字串大於任何項目在清單中,所以插入位置在清單結尾處。 using System; using System.Collections.Generic; public class Example { public static void Main() { List dinosaurs = new List(); dinosaurs.Add("Pachycephalosaurus"); dinosaurs.Add("Amargasaurus"); dinosaurs.Add("Mamenchisaurus"); dinosaurs.Add("Deinonychus"); Console.WriteLine(); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } Console.WriteLine("\nSort"); dinosaurs.Sort(); Console.WriteLine(); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } Console.WriteLine("\nBinarySearch and Insert \"Coelophysis\":"); int index = dinosaurs.BinarySearch("Coelophysis"); if (index < 0) { dinosaurs.Insert(~index, "Coelophysis"); } Console.WriteLine(); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } Console.WriteLine("\nBinarySearch and Insert \"Tyrannosaurus\":"); index = dinosaurs.BinarySearch("Tyrannosaurus"); if (index < 0) { dinosaurs.Insert(~index, "Tyrannosaurus"); } Console.WriteLine(); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } } } /* This code example produces the following output: Pachycephalosaurus Amargasaurus Mamenchisaurus Deinonychus Sort Amargasaurus Deinonychus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Coelophysis": Amargasaurus Coelophysis Deinonychus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Tyrannosaurus": Amargasaurus Coelophysis Deinonychus Mamenchisaurus Pachycephalosaurus Tyrannosaurus */