在C#中,若是想要爲List
實現排序,那麼須要實現一個接口 apiIComparer
接口,從而根據接口中定義的方法來進行排序。
在這裏給出關於IComparer
的官方參考文檔 IComparer參考文檔
在這裏我本身經過對分數進行以下的規則排序:less
// 分數排序 public class Score { private int chinese; private int math; private int english; public Score(int ch, int math, int eng) { chinese = ch; this.math = math; english = eng; } public bool IsSame(Score score) { if (chinese == score.chinese && english == score.english && math == score.math) { return true; } return false; } public int Chinese { get { return chinese; } set { if (value != chinese) chinese = value; } } public int English { get { return english; } set { if (value != english) english = value; } } public int Math { get { return math; } set { if (value != math) math = value; } } } public class ScoreComparer : IComparer<Score> { public int Compare(Score x, Score y) { if (x.IsSame(y)) { return 0; } // compare chinese score firstly if (x.Chinese > y.Chinese) { return 1; } else if (x.Chinese < y.Chinese) { return -1; } // if chinese score is same, we should compare math score secondly if (x.Math > y.Math) { return 1; } else if (x.Math < y.Math) { return -1; } // if chinese and math are same, we should compare english score finally if (x.English > y.English) { return 1; } else if (x.English < y.English) { return -1; } return 0; } } public class TestSort { public static void Test() { List<Score> ts = new List<Score>(); // init ts with some score ts.Add(new Score(80, 88, 45)); ts.Add(new Score(80, 86, 45)); ts.Add(new Score(83, 85, 90)); ts.Add(new Score(83, 85, 40)); ts.Add(new Score(89, 88, 49)); ts.Add(new Score(76, 89, 49)); ts.Add(new Score(73, 88, 49)); ts.Add(new Score(73, 82, 49)); // before sort output all elements in ts Console.WriteLine("Before sort, all score as follows: chinese\tmath\tenglish\t\n"); foreach (var val in ts) { Console.WriteLine("\t\t\t\t\t" + val.Chinese + "\t" + val.Math + "\t" + val.English + "\n"); } // after sort output all elements in ts SortScore(ts); Console.WriteLine("After sort, all score as follows: chinese\tmath\tenglish\t\n"); foreach (var val in ts) { Console.WriteLine("\t\t\t\t\t" + val.Chinese + "\t" + val.Math + "\t" + val.English + "\n"); } } public static void SortScore(List<Score> scoreList) { scoreList.Sort(new ScoreComparer()); } }
在代碼中關於IComparer
接口的定義中實現有返回值的肯定,在這我給出官方的接口定義代碼,從註釋中能夠知道返回值表明什麼意義:this
namespace System.Collections.Generic { // // Summary: // Defines a method that a type implements to compare two objects. // // Type parameters: // T: // The type of objects to compare. public interface IComparer<in T> { // // Summary: // Compares two objects and returns a value indicating whether one is less than, // equal to, or greater than the other. // // Parameters: // x: // The first object to compare. // // y: // The second object to compare. // // Returns: // A signed integer that indicates the relative values of x and y, as shown in the // following table.Value Meaning Less than zero x is less than y.Zero x equals y.Greater // than zero x is greater than y. int Compare(T x, T y); } }
Before sort, all score as follows: chinese math english 80 88 45 80 86 45 83 85 90 83 85 40 89 88 49 76 89 49 73 88 49 73 82 49 After sort, all score as follows: chinese math english 73 82 49 73 88 49 76 89 49 80 86 45 80 88 45 83 85 40 83 85 90 89 88 49