C#對(duì)集合進(jìn)行排序
先來看看下面List<T>泛型集合的排序例子:
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) { List<int> list = new List<int>(); 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è)重載的方法??梢詡鬟f給它的參數(shù)有泛型委托Comparison<T> comparison和泛型接口IComparer<T> comparer,以及一個(gè)范圍值和泛型接口IComparer<T> comparer。只有集合中的元素實(shí)現(xiàn)了IComparable<T>接口,才能使用不帶參數(shù)的Sort()方法。我們?cè)谶@里實(shí)現(xiàn)IComparer<T>接口來創(chuàng)建一個(gè)自定義類型的排序功能。
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 { /// <summary> /// 排序的種類 /// </summary> 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 { /// <summary> /// StudentComparer自定義排序規(guī)則類實(shí)現(xiàn)IComparable接口 /// </summary> public class StudentComparer : IComparer<Student> { private CompareType _compareType; /// <summary> /// 通過構(gòu)造函數(shù)給_compareType賦值 /// </summary> /// <param name="compareType"></param> public StudentComparer(CompareType compareType) { _compareType = compareType; } /// <summary> /// 實(shí)現(xiàn)IComparer接口的Compare /// </summary> /// <param name="other"></param> /// <returns></returns> 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("無(wú)效的比較類型"); } } } }
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) { //List<int> list = new List<int>(); //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<Student> list = new List<Student>() { 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) { //List<int> list = new List<int>(); //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<Student> list = new List<Student>() { 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é)果:
到此這篇關(guān)于C#對(duì)集合進(jìn)行排序的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#/VB.NET 給Excel添加、刪除數(shù)字簽名的方法
這篇文章主要介紹了C#/VB.NET 給Excel添加、刪除數(shù)字簽名的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11C#使用foreach語(yǔ)句遍歷隊(duì)列(Queue)的方法
這篇文章主要介紹了C#使用foreach語(yǔ)句遍歷隊(duì)列(Queue)的方法,涉及foreach語(yǔ)句的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04opencvsharp瑕疵檢測(cè)的實(shí)現(xiàn)示例
本文主要介紹了opencvsharp瑕疵檢測(cè)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05c#使用htmlagilitypack解析html格式字符串
這篇文章主要介紹了c#使用htmlagilitypack解析html格式字符串的示例,需要的朋友可以參考下2014-03-03c# 用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入
這篇文章主要介紹了c# 用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入的步驟,幫助大家更好的理解和使用c#中的Dictionary類,感興趣的朋友可以了解下2021-02-02C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法
這篇文章主要介紹了C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法,以一個(gè)完整的猜拳游戲?yàn)槔v述了C#面向?qū)ο蟪绦蛟O(shè)計(jì)的具體實(shí)現(xiàn)步驟,具有一定的學(xué)習(xí)與借鑒價(jià)值,需要的朋友可以參考下2014-11-11