C#實現(xiàn)List.Sort()使用小計
1、使用List.Sort()對基本數(shù)值類型數(shù)據(jù)進行排序
案例:對存放int數(shù)據(jù)的List進行排序
其實C#中的List的Sort函數(shù)中的比較函數(shù)CompareTo有三種結(jié)果 1, -1 ,0分別代表大,小,相等。默認(rèn)List的排序是升序排序。
舉個例子:在比較函數(shù)CompareTo()中,如果 x>y return 1;則是按照升序排列。如果x>y return -1;則是按照降序排列。這就是1和-1大小的含義。其實你非要這么寫 x<y return 1;則也表示降序排列。不過大家一般習(xí)慣x>y return 1;升序,如果想要降序只需return -1;即可。
Tips:系統(tǒng)List默認(rèn)的排序是升序,如果你想要降序,可以直接在比較函數(shù)前面加個負號,把返回結(jié)果由1變成-1即可。例如:
List<int> list = new List<int>() { 2, 1, 3, 4, 5 };
list.Sort((x, y) => -x.CompareTo(y));
for (int i = 0; i < list.Count(); i++)
{
Console.Write(list[i] + " ");
}
Console.WriteLine();
2、使用List.Sort()對非數(shù)值類型、string、結(jié)構(gòu)體、對象等或者官方未實現(xiàn)IComparable接口的類型進行排序
案例:
假設(shè)我們目前有一個Person類和存放Person類數(shù)據(jù)的List,如下
public class Person
{
public int id;
public string name;
public Person()
{
id = 0;
name = "name";
}
public Person(int id_, string name_)
{
id = id_;
name = name_;
}
}
static void Main(string[] args)
{
List<Person> list2 = new List<Person>();
list2.Add(new Person(10001, "Tom"));
list2.Add(new Person(10003, "Jerry"));
list2.Add(new Person(10002, "Ben"));
list2.Add(new Person(10005, "Cat"));
list2.Add(new Person(10004, "Danny"));
}
假設(shè)我們現(xiàn)在的需求是將這個其按照id從小到大進行排序。
我們有兩種辦法:
方法一:實現(xiàn)IComparable接口重寫CompareTo方法
代碼:
public class Person : IComparable<Person>
{
public int id;
public string name;
public Person()
{
id = 0;
name = "name";
}
public Person(int id_, string name_)
{
id = id_;
name = name_;
}
public int CompareTo(Person obj_)
{
if (this.id > obj_.id)
return 1;
else
return -1;
}
}
static void Main(string[] args)
{
List<int> list = new List<int>() { 2, 1, 3, 4, 5 };
list.Sort((x, y) => -x.CompareTo(y));
for (int i = 0; i < list.Count(); i++)
{
Console.Write(list[i] + " ");
}
Console.WriteLine();
Console.WriteLine();
List<Person> list2 = new List<Person>();
list2.Add(new Person(10001, "Tom"));
list2.Add(new Person(10003, "Jerry"));
list2.Add(new Person(10002, "Ben"));
list2.Add(new Person(10005, "Cat"));
list2.Add(new Person(10004, "Danny"));
// List.Sort自定義對象結(jié)構(gòu)體排序規(guī)則的第一種辦法:在類中實現(xiàn)IComparable接口
list2.Sort();
for (int i = 0; i < list2.Count(); i++)
{
Console.WriteLine(list2[i].id + " " + list2[i].name);
}
Console.WriteLine();
}
方法二:編寫一個排序規(guī)則的函數(shù),調(diào)用LIst.Sort()時將其傳入
代碼:
public class Person
{
public int id;
public string name;
public Person()
{
id = 0;
name = "name";
}
public Person(int id_, string name_)
{
id = id_;
name = name_;
}
}
private static int CmpFun(Person a, Person b)
{
if (a.id > b.id)
return 1;
else
return -1;
}
static void Main(string[] args)
{
List<Person> list2 = new List<Person>();
list2.Add(new Person(10001, "Tom"));
list2.Add(new Person(10003, "Jerry"));
list2.Add(new Person(10002, "Ben"));
list2.Add(new Person(10005, "Cat"));
list2.Add(new Person(10004, "Danny"));
// List.Sort自定義對象結(jié)構(gòu)體排序規(guī)則的第二種辦法:寫一個排序的規(guī)則函數(shù),將其傳入
list2.Sort(CmpFun);
for (int i = 0; i < list2.Count(); i++)
{
Console.WriteLine(list2[i].id + " " + list2[i].name);
}
Console.WriteLine();
}
請注意,在不帶括號的情況下使用方法名稱。 將方法用作參數(shù)會告知編譯器將方法引用轉(zhuǎn)換為可以用作委托調(diào)用目標(biāo)的引用,并將該方法作為調(diào)用目標(biāo)進行附加。
到此這篇關(guān)于C#實現(xiàn)List.Sort()使用小計的文章就介紹到這了,更多相關(guān)C# List.Sort()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中Dictionary<TKey,TValue>排序方式的實現(xiàn)
這篇文章主要介紹了C#中Dictionary<TKey,TValue>排序方式的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
C# Newtonsoft.Json 解析多嵌套json 進行反序列化的實例
這篇文章主要介紹了C# Newtonsoft.Json 解析多嵌套json 進行反序列化的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
C#使用SignalR實現(xiàn)與前端vue實時通信的示例代碼
SignalR 是 ASP.NET Core 的一個庫,它簡化了在應(yīng)用程序中添加實時通信的過程,無論是聊天應(yīng)用、實時游戲還是協(xié)作工具,SignalR 都能提供高效且易于實現(xiàn)的解決方案,本文給大家介紹了C#使用SignalR實現(xiàn)與前端vue實時通信的實現(xiàn),需要的朋友可以參考下2024-10-10
C#編程實現(xiàn)動態(tài)改變配置文件信息的方法
這篇文章主要介紹了C#編程實現(xiàn)動態(tài)改變配置文件信息的方法,涉及C#針對xml格式文件的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06
C# Windows API應(yīng)用之基于FlashWindowEx實現(xiàn)窗口閃爍的方法
這篇文章主要介紹了C# Windows API應(yīng)用之基于FlashWindowEx實現(xiàn)窗口閃爍的方法,結(jié)合實例形式分析了Windows API函數(shù)FlashWindowEx的功能、定義及實現(xiàn)窗口閃爍的相關(guān)技巧,需要的朋友可以參考下2016-08-08

