網(wǎng)站首頁 編程語言 正文
先來看看下面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(); } } }
輸出結(jié)果:
從上面的截圖中可以看出,Sort()方法默認(rèn)按照元素的大小進(jìn)行從小到大的排序,為什么調(diào)用Sort()方法就能按照元素的大小進(jìn)行從小到大的排序呢?其實(shí)現(xiàn)原理是什么呢?我們能不能自定義排序規(guī)則呢?帶著這些問題,我們先來看看Sort()方法的定義,在Sort()方法上面按F12轉(zhuǎn)到定義:
從截圖中可以看出,Sort()方法使用了幾個(gè)重載的方法。可以傳遞給它的參數(shù)有泛型委托Comparison
1、定義一個(gè)Student類,包括姓名和分?jǐn)?shù)兩個(gè)屬性,可以按照姓名或分?jǐn)?shù)進(jìn)行排序,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、在定義一個(gè)枚舉,表示排序的種類,即是按照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、實(shí)現(xiàn)IComparer接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CustomerSort { ////// StudentComparer自定義排序規(guī)則類實(shí)現(xiàn)IComparable接口 /// public class StudentComparer : IComparer{ private CompareType _compareType; /// /// 通過構(gòu)造函數(shù)給_compareType賦值 /// /// public StudentComparer(CompareType compareType) { _compareType = compareType; } ////// 實(shí)現(xiàn)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()方法中調(diào)用:
先按照Name進(jìn)行排序:
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(); } } }
?結(jié)果:
在按照Score進(jìn)行排序:
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(); } } }
結(jié)果:
原文鏈接:https://www.cnblogs.com/dotnet261010/p/9278851.html
相關(guān)推薦
- 2022-05-06 mac brew 啟動(dòng)服務(wù)時(shí)報(bào)錯(cuò)“Bootstrap failed: 5: Input/output
- 2022-12-23 利用Python實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的示例代碼_python
- 2023-03-27 Python?tkinter中l(wèi)abel控件動(dòng)態(tài)改變值問題_python
- 2022-07-25 C++全面精通類與對(duì)象_C 語言
- 2022-10-05 WPF實(shí)現(xiàn)好看的Loading動(dòng)畫的示例代碼_C#教程
- 2022-04-14 k8s安裝過程遇到的問題
- 2022-05-03 詳解利用python-highcharts庫繪制交互式可視化圖表_python
- 2022-05-13 python list.sort()方法排序一探究竟
- 最近更新
-
- 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)證過濾器
- 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)程分支