C#中對(duì)集合排序的三種方式
對(duì)集合排序,可能最先想到的是使用OrderBy方法。
class Program { static void Main(string[] args) { IEnumerable<Student> result = GetStudents().OrderBy(r => r.Score); foreach (var item in result) { Console.WriteLine(item.Name + "--" + item.Score); } Console.ReadKey(); } private static List<Student> GetStudents() { return new List<Student>() { new Student(){Id = 1, Name = "張三",Age = 15, Score = 80}, new Student(){Id = 2, Name = "李四",Age = 16, Score = 70}, new Student(){Id = 3, Name = "趙武",Age = 14, Score = 90} }; } } public class Student { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public int Score { get; set; } }
以上,OrderBy返回的類型是IEnumerable<Student>。
如果想使用List<T>的Sort方法,就需要讓Student實(shí)現(xiàn)IComparable<Student>接口。
class Program { static void Main(string[] args) { List<Student> result = GetStudents(); result.Sort(); foreach (var item in result) { Console.WriteLine(item.Name + "--" + item.Score); } Console.ReadKey(); } private static List<Student> GetStudents() { return new List<Student>() { new Student(){Id = 1, Name = "張三",Age = 15, Score = 80}, new Student(){Id = 2, Name = "李四",Age = 16, Score = 70}, new Student(){Id = 3, Name = "趙武",Age = 14, Score = 90} }; } } public class Student : IComparable<Student> { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public int Score { get; set; } public int CompareTo(Student other) { return this.Score.CompareTo(other.Score); } }
讓Student實(shí)現(xiàn)IComparable<Student>接口固然很好,如果Student是一個(gè)密封類,我們無法讓其實(shí)現(xiàn)IComparable<Student>接口呢?不用擔(dān)心,Sort方法提供了一個(gè)重載,可以接收IComparer接口類型。
class Program { static void Main(string[] args) { List<Student> result = GetStudents(); result.Sort(new StudentSorter()); foreach (var item in result) { Console.WriteLine(item.Name + "--" + item.Score); } Console.ReadKey(); } private static List<Student> GetStudents() { return new List<Student>() { new Student(){Id = 1, Name = "張三",Age = 15, Score = 80}, new Student(){Id = 2, Name = "李四",Age = 16, Score = 70}, new Student(){Id = 3, Name = "趙武",Age = 14, Score = 90} }; } } public class Student { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public int Score { get; set; } } public class StudentSorter : IComparer<Student> { public int Compare(Student x, Student y) { return x.Score.CompareTo(y.Score); } }
綜上,如果我們想對(duì)一個(gè)集合排序,大致有三種方式:
1、使用OrderBy方法,返回IEnumerable<T>類型。
2、讓集合元素實(shí)現(xiàn)IComparable<T>接口,再使用Sort方法,返回void。
3、集合元素不實(shí)現(xiàn)IComparable<T>接口,針對(duì)集合元素類型寫一個(gè)實(shí)現(xiàn)IComparer<T>接口的類,把該類實(shí)例作為Sort方法的參數(shù)。
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
C#實(shí)現(xiàn)基于加減按鈕形式控制系統(tǒng)音量及靜音的方法
這篇文章主要介紹了C#實(shí)現(xiàn)基于加減按鈕形式控制系統(tǒng)音量及靜音的方法,涉及C#引用user32.dll動(dòng)態(tài)鏈接庫操作系統(tǒng)音量的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10C# Winform實(shí)現(xiàn)圓角無鋸齒按鈕
這篇文章主要為大家詳細(xì)介紹了C# Winform實(shí)現(xiàn)圓角無鋸齒按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07C#列出當(dāng)前系統(tǒng)所有正在運(yùn)行程序的方法
這篇文章主要介紹了C#列出當(dāng)前系統(tǒng)所有正在運(yùn)行程序的方法,涉及C#操作系統(tǒng)進(jìn)程的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04C#實(shí)現(xiàn)ComboBox自動(dòng)匹配字符
本文介紹C#如何實(shí)現(xiàn)ComboBox自動(dòng)匹配字符1.采用CustomSource當(dāng)做提示集合2. 直接使用下拉列表中的項(xiàng)作為匹配的集合,需要了解的朋友可以參考下2012-12-12c#使用htmlagilitypack解析html格式字符串
這篇文章主要介紹了c#使用htmlagilitypack解析html格式字符串的示例,需要的朋友可以參考下2014-03-03