網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
對(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è)密封類,我們無(wú)法讓其實(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ù)。
原文鏈接:https://www.cnblogs.com/darrenji/p/4397412.html
相關(guān)推薦
- 2022-11-10 Android開發(fā)Jetpack組件ViewModel與LiveData使用講解_Android
- 2022-07-29 如何通過(guò)redis減庫(kù)存的秒殺場(chǎng)景實(shí)現(xiàn)_Redis
- 2022-10-06 如何利用Redis作為Mybatis的二級(jí)緩存_Redis
- 2022-09-21 Flutter實(shí)現(xiàn)頂部導(dǎo)航欄功能_Android
- 2022-05-27 python繪制棉棒圖的方法詳解_python
- 2022-04-17 h5跳轉(zhuǎn)支付寶小程序
- 2022-05-25 Tomcat配置JMX遠(yuǎn)程連接的詳細(xì)操作_Tomcat
- 2022-05-27 五個(gè)經(jīng)典鏈表OJ題帶你進(jìn)階C++鏈表篇_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支