本篇内容介绍了“c#的List排序方法有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、网站制作、兖州网络推广、成都微信小程序、兖州网络营销、兖州企业策划、兖州品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供兖州建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
//方法一sort排序使用lambda表达式
Listlist = new List () { 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排序
Listlist = new List () { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; list.Reverse();// 反转顺序 list.Sort();// 升序排序
//方法三复杂对象
Listlist = new List (); 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{ 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{ 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 targetList; // Use this for initialization void Start() { } private void Update() { //方法一 if (Input.GetKeyDown(KeyCode.E))//sort排序使用lambda表达式 { List list = new List () { 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 list = new List () { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; list.Reverse();// 反转顺序 list.Sort();// 升序排序 } //方法三 if (Input.GetKeyDown(KeyCode.W))//简单sort排序 { List list = new List (); 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 (); 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 InOrder(List 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 InvertedOrder(List 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; } /// /// List乱序 /// /// ///public List OutOfOrder(List a) { Debug.LogError("****打乱列表****"); List b = new List (); 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排序方法有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!