有时我们会遇到多个字段进行排序,比如先按照英雄品级排序、再按照英雄星级排序、最后按照英雄等级排序等等
这时候不妨使用Linq完成该操作
下面的排序是先按是否存活,再按id排序~~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; class Data { public int id; public bool life; public Data(int id, bool life) { this.id = id; this.life = life; } } public class Test : MonoBehaviour { List<Data> dataList = new List<Data>() { new Data(1, false), new Data(5, true), new Data(3, true), new Data(4, false) }; // Use this for initialization void Start() { // 升序排序 dataList = dataList.OrderBy(i => i.life).ThenBy(i => i.id).ToList(); // 先按life排序,然后再按id排序 for (int i = 0; i < dataList.Count; i++) { Debug.Log("升序:" + dataList[i].life + " " + dataList[i].id); } Debug.Log("~~~~~~~~~~~~~~~~~~~~~"); // 降序排序 dataList = dataList.OrderByDescending(i => i.life).ThenByDescending(i => i.id).ToList(); for (int i = 0; i < dataList.Count; i++) { Debug.Log("降序:" + dataList[i].life + " " + dataList[i].id); } } } |
- 本文固定链接: http://www.u3d8.com/?p=1270
- 转载请注明: 网虫虫 在 u3d8.com 发表过