Unity中常用 : List排序
程序员文章站
2024-03-12 19:40:14
...
-
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; /// <summary> /// 个人信息 /// </summary> public class PersonInfo { public int ID; public int Age; public string Names; public PersonInfo(int id, int age, string name, List<PersonInfo> temp = null) { this.ID = id; this.Age = age; this.Names = name; temp?.Add(this); } } public class ListTools : MonoBehaviour { private List<PersonInfo> Persons = new List<PersonInfo>(); void Start () { //一种添加方法 var a = new PersonInfo(1001, 10, "小东", Persons); //正常添加方法 Persons.Add(new PersonInfo(1002, 15, "小马")); Persons.Add(new PersonInfo(1003, 20, "小牛")); Persons.Add(new PersonInfo(1004, 25, "小洗")); this.SortType(); } public void SortType() { //排序方式:orderby 升序 //会根据age的大小,小的在最上面 排序下来 this.Persons = Persons.OrderBy(x => x.Age).ToList(); //降序, 会根据年龄的大小, 从大往小的牌 this.Persons = Persons.OrderByDescending(x => x.Age).ToList(); //联合使用 this.Persons = Persons.OrderBy(x => x.ID).OrderByDescending(y => y.Age).ToList(); } }
上一篇: react入门(三)——state
下一篇: path 这个东西