国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

C# list的sort排序

這篇具有很好參考價值的文章主要介紹了C# list的sort排序。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

目錄

前言:

值類型的排序:

方法一:直接調(diào)用sort函數(shù)

方法二:通過C# ling表達(dá)式與CompareTo接口配合使用

方法三:降序的實(shí)現(xiàn)

對于自定義類型的sort排序?

方法一:通過實(shí)現(xiàn)IComparable接口重寫CompareTo方法來排序

方法二:通過ling表達(dá)式實(shí)現(xiàn)?


前言:

????????有時需要對List列表中內(nèi)容進(jìn)行排序,List 的Sort方法排序有三種結(jié)果 1,0,-1分別表示大于,等于,小于。List的sort的方法如何使用,如何實(shí)現(xiàn)降序和升序

值類型的排序:

方法一:直接調(diào)用sort函數(shù)

?static void Main(string[] args)
? ?{

? ? ? ? ? ? List<int> sortList = new List<int>() { 100, 101, 50, 4 };
? ? ? ? ? ? sortList.Sort();

? ? ? ? ? ? foreach (var i in sortList)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write(i);
? ? ? ? ? ? ? ? Console.Write(",");
? ? ? ? ? ? }
? ? ? ? ? ? sortList.Sort((a, b) => a.CompareTo(b));
? ? ? ? ? ? Thread.Sleep(10000);
? ? ? ? }

輸出結(jié)果: 4,50,100,101,默認(rèn)情況下,通過sort排序是升序?

方法二:通過C# ling表達(dá)式與CompareTo接口配合使用

sortList.Sort((a, b) => a.CompareTo(b)); //升序排序,與sortList.Sort()結(jié)果一樣

方法三:降序的實(shí)現(xiàn)

可以直接在對比較結(jié)果取負(fù)值

?sortList.Sort((a, b) => -a.CompareTo(b)); //結(jié)果取負(fù)值,升序就變成了降序了

查看int32源碼我們看到,int32 是實(shí)現(xiàn)了CompareTo接口的

public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int>
? {
? ? internal int m_value;
? ? [__DynamicallyInvokable]
? ? public const int MaxValue = 2147483647;
? ? [__DynamicallyInvokable]
? ? public const int MinValue = -2147483648;

? ? public int CompareTo(object value)
? ? {
? ? ? if (value == null)
? ? ? ? return 1;
? ? ? if (!(value is int num))
? ? ? ? throw new ArgumentException(Environment.GetResourceString("Arg_MustBeInt32"));
? ? ? if (this < num)
? ? ? ? return -1;
? ? ? return this > num ? 1 : 0;
? ? }

? ? [__DynamicallyInvokable]
? ? public int CompareTo(int value)
? ? {
? ? ? if (this < value)
? ? ? ? return -1;
? ? ? return this > value ? 1 : 0;
? ? }

}

對于自定義類型的sort排序?

方法一:通過實(shí)現(xiàn)IComparable接口重寫CompareTo方法來排序

? ? public class Dog : IComparable<Dog>

? ? {
? ? ? ? private float _price;

? ? ? ? public float price => _price;
? ? ? ? public Dog(float price)
? ? ? ? {
? ? ? ? ? ? _price = price;
? ? ? ? }

? ? ? ? public int CompareTo(Dog other)
? ? ? ? {
? ? ? ? ? ? if (_price > other.price)
? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? if (_price == other.price)
? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? return -1;
? ? ? ? }

? ? ? ? public override string ToString()
? ? ? ? {
? ? ? ? ? ? return _price.ToString();
? ? ? ? }
? ? }

?文章來源地址http://www.zghlxwxcb.cn/news/detail-615468.html

? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? List<Dog> dogs = new List<Dog>();
? ? ? ? ? ? dogs.Add(new Dog(12));
? ? ? ? ? ? dogs.Add(new Dog(10));
? ? ? ? ? ? dogs.Add(new Dog(14));

? ? ? ? ? ? dogs.Sort();

? ? ? ? ? ? foreach (var i in dogs)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write(i);
? ? ? ? ? ? ? ? Console.Write(",");
? ? ? ? ? ? }
? ? ? ? ? ? Thread.Sleep(10000);
? ? ? ? }

輸出結(jié)果:為 10,12,14,

方法二:通過ling表達(dá)式實(shí)現(xiàn)?

? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? List<Dog> dogs = new List<Dog>();
? ? ? ? ? ? dogs.Add(new Dog(12));
? ? ? ? ? ? dogs.Add(new Dog(10));
? ? ? ? ? ? dogs.Add(new Dog(14));

? ? ? ? ? ? dogs.Sort((a, b) =>
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (a.price > b.price)
? ? ? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? ? ? if (a.price == b.price)
? ? ? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? ? ? return -1;
? ? ? ? ? ? });

? ? ? ? ? ? foreach (var i in dogs)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write(i);
? ? ? ? ? ? ? ? Console.Write(",");
? ? ? ? ? ? }
? ? ? ? ? ? Thread.Sleep(10000);
? ? ? ? }

?

到了這里,關(guān)于C# list的sort排序的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • C#中sort排序相關(guān)用法介紹

    C#中sort排序相關(guān)用法介紹

    ?C#中,List.Sort() 不僅為我們提供了默認(rèn)的排序方法,還為我們提供了4種自定義排序的方法,通過默認(rèn)排序方法,我們無需重寫任何Sort()方法的實(shí)現(xiàn)代碼,就能對單參數(shù)類型的List數(shù)據(jù)進(jìn)行單一規(guī)則的排序,如果通過對這些方法進(jìn)行改進(jìn)我們可以輕松做到對多參數(shù)、多規(guī)則的復(fù)

    2024年02月15日
    瀏覽(82)
  • 筆記--java sort() 方法排序

    筆記--java sort() 方法排序

    最近在刷一道算法題 《字符串重新排序》時,發(fā)現(xiàn)自己有思路但是寫代碼的時候就無從下手了 而且看了答案之后還沒看懂 關(guān)鍵就是基礎(chǔ)不好 對于排序沒有理解(雖然我學(xué)過常用的排序算法 但是都是理念 實(shí)踐少) 從實(shí)踐和原理出發(fā) 重點(diǎn)是從實(shí)踐出發(fā) 探討如何使用 sort()方法

    2024年02月07日
    瀏覽(97)
  • 【筆記】Unity編程(C#語言詳解)

    【筆記】Unity編程(C#語言詳解)

    從靈魂、身體、行為三個層面來看,腳本(Script)為游戲注入了生動的元素。腳本代碼并非獨(dú)立運(yùn)行的程序,它依賴于Unity引擎的運(yùn)行環(huán)境,并需附加到特定對象上。在Unity腳本中,沒有像傳統(tǒng)程序中的\\\"main\\\"函數(shù),而是用于更新游戲循環(huán)中的對象。Unity為腳本提供了API以便訪問

    2024年02月03日
    瀏覽(102)
  • C#,歸并排序算法(Merge Sort Algorithm)的源代碼及數(shù)據(jù)可視化

    C#,歸并排序算法(Merge Sort Algorithm)的源代碼及數(shù)據(jù)可視化

    歸并算法采用非常經(jīng)典的 分治策略 ,每次把序列分成n/2的長度,將問題分解成小問題,由復(fù)雜變簡單。 因?yàn)槭褂昧诉f歸算法,不能用于大數(shù)據(jù)的排序。 核心代碼: using System; using System.Text; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsFormsApp6 { ? ? public parti

    2024年02月02日
    瀏覽(98)
  • Unity學(xué)習(xí)筆記--使用 C# 開發(fā)一個 LRU

    什么是 LRU 在計(jì)算機(jī)系統(tǒng)中,LRU(Least Recently Used,最近最少使用)是一種緩存置換算法。緩存是計(jì)算機(jī)系統(tǒng)中的一種能夠高速獲取數(shù)據(jù)的介質(zhì),而緩存置換算法則是在緩存空間不足時,需要淘汰掉部分緩存數(shù)據(jù)以騰出空間,使得后來需要訪問數(shù)據(jù)能夠有更多的緩存空間可用。

    2024年02月13日
    瀏覽(710)
  • C# 泛型List排序的實(shí)現(xiàn)

    C# 泛型List排序的實(shí)現(xiàn)

    本文主要介紹了C# 泛型List排序的實(shí)現(xiàn),分享給大家,具體如下: 代碼 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 9

    2024年02月12日
    瀏覽(91)
  • C#中List的3種排序方法

    List是C#常用的數(shù)組,它較之前的ArryList更加靈活,解決了Arrylist會出現(xiàn)裝箱和拆箱的不安全問題,它是一種動態(tài)數(shù)組,可以存儲對象或元素的集合。在處理比多的數(shù)據(jù)時,對list排序也非常重要,這樣可以對數(shù)據(jù)更加容易的處理。本文將講述3種list的排序方法。 方法1:Sort方法

    2024年02月13日
    瀏覽(89)
  • C#簡單操作:C#中List常用方法 判斷存在、查找、排序

    C#簡單操作:C#中List常用方法 判斷存在、查找、排序

    目常用List來進(jìn)行數(shù)據(jù)操作管理,有一些方法經(jīng)常百度,所以這里記錄下。 目錄 1. List判斷元素是否存在,返回bool 2. List查找,返回對象 3. List排序 4. 對象屬性打印 5. List 其他方法 ?

    2024年02月11日
    瀏覽(90)
  • 數(shù)組(Arrays)和列表(Lists) — Unity C#

    數(shù)組(Arrays)和列表(Lists) — Unity C#

    數(shù)組是 C# 提供的最基本的集合。將它們視為一組值的容器,在編程術(shù)語中稱為元素,每個值都可以單獨(dú)訪問或修改。 · 數(shù)組可以存儲任何類型的值;所有元素必須屬于同一類型。 · 數(shù)組的長度或元素數(shù)量是在創(chuàng)建時設(shè)置的。 · 如果創(chuàng)建時沒有指定初始值,則每個元素都會被

    2024年01月23日
    瀏覽(95)
  • C 語言實(shí)現(xiàn) C# 中的 List 泛型列表

    //下面是一個簡單的用 C 語言實(shí)現(xiàn) C# 中的 List 泛型列表的示例代碼,代碼中有詳細(xì)的注釋,幫助你理解代碼的實(shí)現(xiàn)細(xì)節(jié)。 人工智能生成的. 以后可以用人工智能實(shí)現(xiàn)很多代碼了. 簡單的活讓它來干.

    2024年02月10日
    瀏覽(88)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包