您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“c#的List排序方法有哪些”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
//方法一sort排序使用lambda表達(dá)式
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));//升序
//方法二簡(jiǎn)單sort排序
List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; list.Reverse();// 反轉(zhuǎn)順序 list.Sort();// 升序排序
//方法三復(fù)雜對(duì)象
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運(yùn)用
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);//升序 }
測(cè)試腳本如下
#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表達(dá)式 { 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))//簡(jiǎn)單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))//簡(jiǎn)單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的運(yùn)用 { 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é)號(hào):" + 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é)號(hào):" + 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) { //隨機(jī)將a中序號(hào)為index的元素作為b中的第一個(gè)元素放入b中 int index = UnityEngine.Random.Range(0, a.Count - 1); //檢測(cè)是否重復(fù),保險(xiǎn)起見 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é)號(hào):" + str); return b; } }
“c#的List排序方法有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。