欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)List.Sort()使用小計

 更新時間:2023年12月12日 09:36:52   作者:bughunter-  
在C#開發(fā)中,List是常見的一種集合類型,其提供了一個 Sort() 方法來實現(xiàn)對集合的排序,本文主要介紹了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分別代表大,小,相等。默認List的排序是升序排序。

舉個例子:在比較函數(shù)CompareTo()中,如果 x>y return 1;則是按照升序排列。如果x>y return -1;則是按照降序排列。這就是1和-1大小的含義。其實你非要這么寫 x<y return 1;則也表示降序排列。不過大家一般習慣x>y return 1;升序,如果想要降序只需return -1;即可。

Tips:系統(tǒng)List默認的排序是升序,如果你想要降序,可以直接在比較函數(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)用目標的引用,并將該方法作為調(diào)用目標進行附加。

到此這篇關(guān)于C#實現(xiàn)List.Sort()使用小計的文章就介紹到這了,更多相關(guān)C# List.Sort()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論