欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

c#的List排序方法有哪些

本篇內(nèi)容介紹了“c#的List排序方法有哪些”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、網(wǎng)站制作、兗州網(wǎng)絡(luò)推廣、成都微信小程序、兗州網(wǎng)絡(luò)營銷、兗州企業(yè)策劃、兗州品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供兗州建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:www.chinadenli.net

 //方法一sort排序使用lambda表達式

List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            list.Sort((x, y) => -x.CompareTo(y));//降序
            list.Sort((x, y) => x.CompareTo(y));//升序

  //方法二簡單sort排序

List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            list.Reverse();// 反轉(zhuǎn)順序          
            list.Sort();// 升序排序

 //方法三復(fù)雜對象

List<Student> list = new List<Student>();
            list.Sort(
                delegate (Student p1, Student p2)
                 {
                     return p1.sno.CompareTo(p2.sno);//升序
                     //return p1.sno == p1.sno ? 0 : (p1.sno > p1.sno) ? 1 : -1;
                 });
            //list.Sort((x, y) => { return x.sno.CompareTo(y.sno); });

 方法四OrdeOrderBy運用

Debug.Log("****順序排列****");
        var tlist = list.OrderBy(t => t.sno).ToList();

Debug.Log("****倒序排列****");
        var tlist = list.OrderByDescending(t => t.sno).ToList();

方法五 chon重寫Comparable

public class Student: IComparable<Student>
{
    public int sno;
    public string name;

    public Student(int sno, string name)
    {
        this.sno = sno;
        this.name = name;
    }

    //重寫的CompareTo方法,根據(jù)Id排序
    public int CompareTo(Student other)
    {
        if (null == other)
        {
            return 1;//空值比較大,返回1
        }
        //return this.Id.CompareTo(other.Id);//升序
        return other.sno.CompareTo(this.sno);//降序
    }
}

或者

public int Compare(Student x, Student y)
    {
        return x.sno.CompareTo(y.sno);//升序
    }

測試腳本如下

#region 模塊信息
// **********************************************************************
// Copyright (C) 2019 Blazors
// Please contact me if you have any questions
// File Name:             Test
// Author:               
// WeChat||QQ:           
// **********************************************************************
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Student: IComparable<Student>
{
    public int sno;
    public string name;

    public Student(int sno, string name)
    {
        this.sno = sno;
        this.name = name;
    }

    //重寫的CompareTo方法,根據(jù)Id排序
    public int CompareTo(Student other)
    {
        if (null == other)
        {
            return 1;//空值比較大,返回1
        }
        //return this.Id.CompareTo(other.Id);//升序
        return other.sno.CompareTo(this.sno);//降序
    }
    public int Compare(Student x, Student y)
    {
        return x.sno.CompareTo(y.sno);//升序
    }

}
public class Test : MonoBehaviour
{
    List<Student> targetList;
    // Use this for initialization
    void Start()
    {
      
    }
    private void Update()
    {
        //方法一
        if (Input.GetKeyDown(KeyCode.E))//sort排序使用lambda表達式
        {
            List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            list.Sort((x, y) => -x.CompareTo(y));//降序
            list.Sort((x, y) => x.CompareTo(y));//升序

           
        }
        //方法二
        if (Input.GetKeyDown(KeyCode.W))//簡單sort排序
        {
            List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            list.Reverse();// 反轉(zhuǎn)順序          
            list.Sort();// 升序排序

        }
        //方法三
        if (Input.GetKeyDown(KeyCode.W))//簡單sort排序
        {
            List<Student> list = new List<Student>();
            list.Sort(
                delegate (Student p1, Student p2)
                 {
                     return p1.sno.CompareTo(p2.sno);
                    
                 });
            //list.Sort((x, y) => { return x.sno.CompareTo(y.sno); });
        }
       
        //方法三
        if (Input.GetKeyDown(KeyCode.Q))//OrderBy的運用
        {
            targetList = new List<Student>();
            for (int i = 0; i < 10; i++)
            {
                targetList.Add(new Student(i, "小明" + i));
            }
            var tList01 = OutOfOrder(targetList);
            var tList02 = InOrder(tList01);
            var tList03 = OutOfOrder(tList02);
            InvertedOrder(tList03);
        }
       
    }
    private List<Student> InOrder(List<Student> list)
    {
        Debug.Log("****順序排列****");
        var tlist = list.OrderBy(t => t.sno).ToList();
        string str = ""; ;
        foreach (var item in tlist)
        {
            str += item.sno;
        }
        Debug.Log("順序后學(xué)號:" + str);
        return tlist;
    }
    private List<Student> InvertedOrder(List<Student> list)
    {
        Debug.Log("****倒序排列****");
        var tlist = list.OrderByDescending(t => t.sno).ToList();
        string str = ""; ;
        foreach (var item in tlist)
        {
            str += item.sno;
        }
        Debug.Log("倒序后學(xué)號:" + str);
        return tlist;
    }


    /// <summary>
    /// List亂序
    /// </summary>
    /// <param name="a"></param>
    /// <returns></returns>
    public List<Student> OutOfOrder(List<Student> a)
    {
        Debug.LogError("****打亂列表****");
        List<Student> b = new List<Student>();
        int countNum = a.Count;
        //使用while循環(huán),保證將a中的全部元素轉(zhuǎn)移到b中而不產(chǎn)生遺漏
        while (b.Count < countNum)
        {
            //隨機將a中序號為index的元素作為b中的第一個元素放入b中
            int index = UnityEngine.Random.Range(0, a.Count - 1);
            //檢測是否重復(fù),保險起見
            if (!b.Contains(a[index]))
            {
                //若b中還沒有此元素,添加到b中
                b.Add(a[index]);
                //成功添加后,將此元素從a中移除,避免重復(fù)取值
                a.Remove(a[index]);
            }
        }
        string str = ""; ;
        foreach (var item in b)
        {
            str += item.sno;
        }
        Debug.Log("亂序后學(xué)號:" + str);
        return b;
    }

}

“c#的List排序方法有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

當前名稱:c#的List排序方法有哪些
轉(zhuǎn)載來源:http://www.chinadenli.net/article34/gghspe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站Google企業(yè)網(wǎng)站制作ChatGPT網(wǎng)站收錄小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管