本篇内容介绍了“c#的List排序方法有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
//方法一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();// 反转顺序 list.Sort();// 升序排序
//方法三复杂对象
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方法,根据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方法,根据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();// 反转顺序 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("顺序后学号:" + 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("倒序后学号:" + 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循环,保证将a中的全部元素转移到b中而不产生遗漏 while (b.Count < countNum) { //随机将a中序号为index的元素作为b中的第一个元素放入b中 int index = UnityEngine.Random.Range(0, a.Count - 1); //检测是否重复,保险起见 if (!b.Contains(a[index])) { //若b中还没有此元素,添加到b中 b.Add(a[index]); //成功添加后,将此元素从a中移除,避免重复取值 a.Remove(a[index]); } } string str = ""; ; foreach (var item in b) { str += item.sno; } Debug.Log("乱序后学号:" + str); return b; } }
“c#的List排序方法有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。