網站首頁 編程語言 正文
對集合排序,可能最先想到的是使用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實現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實現IComparable<Student>接口固然很好,如果Student是一個密封類,我們無法讓其實現IComparable<Student>接口呢?不用擔心,Sort方法提供了一個重載,可以接收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);
}
}
綜上,如果我們想對一個集合排序,大致有三種方式:
1、使用OrderBy方法,返回IEnumerable<T>類型。
2、讓集合元素實現IComparable<T>接口,再使用Sort方法,返回void。
3、集合元素不實現IComparable<T>接口,針對集合元素類型寫一個實現IComparer<T>接口的類,把該類實例作為Sort方法的參數。
原文鏈接:https://www.cnblogs.com/darrenji/p/4397412.html
相關推薦
- 2023-01-20 Django如何實現RBAC權限管理_python
- 2022-07-24 elment-ui的上傳組件圖片不回顯
- 2022-05-12 android okHttp網絡請求封裝
- 2022-07-04 python如何生成任意n階的三對角矩陣_python
- 2022-07-04 Python基礎之矩陣輸入的實例_python
- 2022-11-02 解決SecureCRT通過SSH連接Ubuntu時vi命令有多余的m的問題_相關技巧
- 2023-01-31 React組件通信淺析_React
- 2022-08-21 Python命令行庫click的具體使用_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支