網站首頁 編程語言 正文
先來看看下面List
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CustomerSort { class Program { static void Main(string[] args) { Listlist = new List (); list.Add(1); list.Add(5); list.Add(2); list.Add(6); list.Add(3); list.Add(4); Console.WriteLine("*****排序前*****"); foreach (var item in list) { Console.WriteLine(item.ToString()); } list.Sort(); Console.WriteLine("*****排序后*****"); foreach (var item in list) { Console.WriteLine(item.ToString()); } Console.ReadKey(); } } }
輸出結果:
從上面的截圖中可以看出,Sort()方法默認按照元素的大小進行從小到大的排序,為什么調用Sort()方法就能按照元素的大小進行從小到大的排序呢?其實現原理是什么呢?我們能不能自定義排序規則呢?帶著這些問題,我們先來看看Sort()方法的定義,在Sort()方法上面按F12轉到定義:
從截圖中可以看出,Sort()方法使用了幾個重載的方法。可以傳遞給它的參數有泛型委托Comparison
1、定義一個Student類,包括姓名和分數兩個屬性,可以按照姓名或分數進行排序,Student類定義如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CustomerSort { public class Student { public string Name { get; set; } public double Score { get; set; } } }
2、在定義一個枚舉,表示排序的種類,即是按照Name排序還是按照Score排序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CustomerSort { ////// 排序的種類 /// public enum CompareType { Name, Score } }
3、實現IComparer接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CustomerSort { ////// StudentComparer自定義排序規則類實現IComparable接口 /// public class StudentComparer : IComparer{ private CompareType _compareType; /// /// 通過構造函數給_compareType賦值 /// /// public StudentComparer(CompareType compareType) { _compareType = compareType; } ////// 實現IComparer接口的Compare /// /// ///public int Compare(Student x, Student y) { if (x == null && y == null) { return 0; } if (x == null) { return -1; } if (y == null) { return 1; } switch (_compareType) { case CompareType.Name: return string.Compare(x.Name, y.Name); break; case CompareType.Score: return x.Score.CompareTo(y.Score); break; default: throw new ArgumentException("無效的比較類型"); } } } }
4、在Main()方法中調用:
先按照Name進行排序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CustomerSort { class Program { static void Main(string[] args) { //Listlist = new List (); //list.Add(1); //list.Add(5); //list.Add(2); //list.Add(6); //list.Add(3); //list.Add(4); //Console.WriteLine("*****排序前*****"); //foreach (var item in list) //{ // Console.WriteLine(item.ToString()); //} //list.Sort(); //Console.WriteLine("*****排序后*****"); //foreach (var item in list) //{ // Console.WriteLine(item.ToString()); //} List list = new List () { new Student() { Name="Tom", Score=98 } , new Student() { Name="Kevin", Score=69 } , new Student() { Name="Leo", Score=81 } }; Console.WriteLine("*****排序前*****"); foreach (var item in list) { Console.WriteLine(item.Name); } list.Sort(new StudentComparer(CompareType.Name)); Console.WriteLine("*****排序后*****"); foreach (var item in list) { Console.WriteLine(item.Name); } //Console.WriteLine("***按照Score排序***"); Console.ReadKey(); } } }
?結果:
在按照Score進行排序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CustomerSort { class Program { static void Main(string[] args) { //Listlist = new List (); //list.Add(1); //list.Add(5); //list.Add(2); //list.Add(6); //list.Add(3); //list.Add(4); //Console.WriteLine("*****排序前*****"); //foreach (var item in list) //{ // Console.WriteLine(item.ToString()); //} //list.Sort(); //Console.WriteLine("*****排序后*****"); //foreach (var item in list) //{ // Console.WriteLine(item.ToString()); //} List list = new List () { new Student() { Name="Tom", Score=98 } , new Student() { Name="Kevin", Score=69 } , new Student() { Name="Leo", Score=81 } }; //Console.WriteLine("*****排序前*****"); //foreach (var item in list) //{ // Console.WriteLine(item.Name); //} //list.Sort(new StudentComparer(CompareType.Name)); //Console.WriteLine("*****排序后*****"); //foreach (var item in list) //{ // Console.WriteLine(item.Name); //} Console.WriteLine("*****排序前*****"); foreach (var item in list) { Console.WriteLine(item.Score); } list.Sort(new StudentComparer(CompareType.Name)); Console.WriteLine("*****排序后*****"); foreach (var item in list) { Console.WriteLine(item.Score); } Console.ReadKey(); } } }
結果:
原文鏈接:https://www.cnblogs.com/dotnet261010/p/9278851.html
相關推薦
- 2022-02-11 Android之Compose頁面切換動畫介紹_Android
- 2022-01-21 什么是前端開發?什么是后端開發?
- 2022-04-28 C++實現簡單班級成績管理系統_C 語言
- 2023-01-03 go?doudou開發gRPC服務快速上手實現詳解_Golang
- 2023-01-10 golang實現簡單的tcp數據傳輸_Golang
- 2023-06-20 Redis?設置密碼無效問題解決_Redis
- 2023-01-21 C++實現逆波蘭表達式的例題詳解_C 語言
- 2022-08-26 Redis哨兵模式實現一主二從三哨兵_Redis
- 最近更新
-
- 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同步修改后的遠程分支