C#通過(guò)IComparable實(shí)現(xiàn)ListT.sort()排序
本文實(shí)例講述了C#通過(guò)IComparable實(shí)現(xiàn)ListT.sort()排序的方法,分享給大家供大家參考之用。具體方法如下:
通常來(lái)說(shuō),List<T>.sort()可以實(shí)現(xiàn)對(duì)T的排序,比如List<int>.sort()執(zhí)行后集合會(huì)按照int從小到大排序。如果T是一個(gè)自定義的Object,可是我們想按照自己的方式來(lái)排序,那該怎么辦呢,其實(shí)可以用過(guò)IComparable接口重寫CompareTo方法來(lái)實(shí)現(xiàn)。流程如下:
一、第一步我們申明一個(gè)類Person但是要繼承IComparable接口:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestIComparable { public class Person : IComparable<Person> { public string Name { get; set; } public int Age { get; set; } public int CompareTo(Person obj) { int result; if (this.Name == obj.Name && this.Age == obj.Age) { result = 0; } else { if (this.Name.CompareTo(obj.Name) > 0) { result = 1; } else if (this.Name == obj.Name && this.Age > obj.Age) { result = 1; } else { result = -1; } } return result; } public override string ToString() { return this.Name + "-" + this.Age; } } }
二、然后在主函數(shù)里面調(diào)用sort方法即可.類就會(huì)按照姓名從小到大,如果姓名相同則按照年齡從小到大排序了。
public class Program { public static void Main(string[] args) { List<Person> lstPerson = new List<Person>(); lstPerson.Add(new Person(){ Name="Bob",Age=19}); lstPerson.Add(new Person(){ Name="Mary",Age=18}); lstPerson.Add(new Person() { Name = "Mary", Age = 17 }); lstPerson.Add(new Person(){ Name="Lily",Age=20}); lstPerson.Sort(); Console.ReadKey(); } }
三、如果不繼承IComparable接口,我們?cè)撊绾螌?shí)現(xiàn)排序呢??梢允褂肔inq來(lái)實(shí)現(xiàn)。其實(shí)效果是一樣的,只是如果類的集合要經(jīng)常排序的話,建議使用繼承接口的方法,這樣可以簡(jiǎn)化sort的代碼,而且更容易讓人看懂。
public static void Main(string[] args) { List<Person> lstPerson = new List<Person>(); lstPerson.Add(new Person(){ Name="Bob",Age=19}); lstPerson.Add(new Person(){ Name="Mary",Age=18}); lstPerson.Add(new Person() { Name = "Mary", Age = 17 }); lstPerson.Add(new Person(){ Name="Lily",Age=20}); lstPerson.Sort((x,y) => { int result; if (x.Name == y.Name && x.Age == y.Age) { result = 0; } else { if (x.Name.CompareTo(y.Name) > 0) { result = 1; } else if (x.Name == y.Name && x.Age > y.Age) { result = 1; } else { result = -1; } } return result; }); Console.ReadKey(); }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
c#生成站點(diǎn)地圖(SiteMapPath)文件示例程序
這篇文章主要介紹了c#生成站點(diǎn)地圖(SiteMapPath)文件的示例,大家參考使用2013-11-11FTPClientHelper輔助類 實(shí)現(xiàn)文件上傳,目錄操作,下載等操作
這篇文章主要分享了一個(gè)FTPClientHelper輔助類和介紹了常用的FTP命令,需要的朋友可以參考下。2016-06-06c# 圓形識(shí)別方案和直線識(shí)別方案的參考示例
這篇文章主要介紹了c# 圓形識(shí)別方案和直線識(shí)別方案的實(shí)現(xiàn)示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03C#實(shí)現(xiàn)在listview中插入圖片實(shí)例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)在listview中插入圖片實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03C# 導(dǎo)出Excel的6種簡(jiǎn)單方法實(shí)現(xiàn)
C# 導(dǎo)出 Excel 的6種簡(jiǎn)單方法:數(shù)據(jù)表導(dǎo)出到 Excel,對(duì)象集合導(dǎo)出到 Excel,數(shù)據(jù)庫(kù)導(dǎo)出到 Excel,微軟網(wǎng)格控件導(dǎo)出到 Excel,數(shù)組導(dǎo)出到 Excel,CSV 導(dǎo)出到 Excel,你都會(huì)了嗎?需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09C#使用Socket實(shí)現(xiàn)發(fā)送和接收?qǐng)D片的方法
這篇文章主要介紹了C#使用Socket實(shí)現(xiàn)發(fā)送和接收?qǐng)D片的方法,涉及C#操作socket發(fā)送與接收文件的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04