2025年Unity 基础 之 List Sort 升降排序,List 对多数按权重排序,List 对自定义类 排序

Unity 基础 之 List Sort 升降排序,List 对多数按权重排序,List 对自定义类 排序Unity 基础 之 List Sort 升降排序 List 对多数按权重排序 List 对自定义类 排序 目录 Unity 基础 之 List Sort 升降排序 List 对多数按权重排序 List 对自定义类 排序 一 简单介绍 二 基本概念 三 List Sort 使用举例 1

大家好,我是讯享网,很高兴认识大家。

Unity 基础  之  List  Sort 升降排序,List 对多数按权重排序,List 对自定义类 排序

 

目录

Unity 基础  之  List  Sort 升降排序,List 对多数按权重排序,List 对自定义类 排序

一、简单介绍

二、基本概念

三、List Sort 使用举例

1)List 基础数比较

2)List 对数权重比较

3)List 自定义类的字段比较


 

一、简单介绍

Unity中的一些基础知识点。

本片介绍 List 在作为 参数传递的时候的一些之变化情况。

 

二、基本概念

所属命名空间:System.Collections.Generic


讯享网

public class List : IList, ICollection, IEnumerable, IList, ICollection, IEnumerable

List类是 ArrayList 类的泛型等效类。该类使用大小可按需动态增加的数组实现 IList 泛型接口。

List.Sort:

(1)有三种结果 1,-1,0分别是大,小,相等;

(2)升序降序比较,默认List的排序是升序排序;

(3)如果要降序排序,也很简单,只需要在前面加一个负号

 

三、List Sort 使用举例

1)List 基础数比较

List<int> tmp = new List<int>(){5,11,22,11,4}; // 升序 tmp.Sort((x, y) => x.CompareTo(y)); // 4,5,11,11,22 // 降序 tmp.Sort((x, y) => -x.CompareTo(y)); // 22,11,11,5,4 Console.WriteLine(tmp); 

讯享网

 

2)List 对数权重比较

对于非数值类型比较用.CompareTo(...),基于IComparable接口。基本上C#的值类型都有实现这个接口,包括string。
而数值类型也可以自己比较。排序时左右两个变量必须是左-比较-右,切记不可反过来比较。
sort方法官方推荐的 命名方式是x(左),y(右) 。对于复杂的比较 可以分出来,单独写成函数
多权重比较
假设需要tuple里item2的值优先于item1。这个时候只要给比较结果*2即可。
 

讯享网List<Tuple<int, int>> tmp = new List<Tuple<int, int>>() { new Tuple<int,int>(5,0), new Tuple<int,int>(52,1), new Tuple<int,int>(23,1), new Tuple<int,int>(22,3), new Tuple<int,int>(100,2), }; tmp.Sort((x, y) => -(x.Item1.CompareTo(y.Item1) + x.Item2.CompareTo(y.Item2) * 2)); Console.WriteLine(tmp); //22,3 //100,2 //52,1 //23,1 //5,0 

 

3)List 自定义类的字段比较

using System.Collections; using System.Collections.Generic; using UnityEngine; public class ListSort : MonoBehaviour { /// <summary> /// 自定义的类 /// </summary> public class customClass { public string name; public float salary; public int level; public customClass(string name, float salary, int level) { this.name = name; this.salary = salary; this.level = level; } public string toString() { return "name : "+name + " , salary : " + salary + " , level :" + level ; } } // Start is called before the first frame update void Start() { List<customClass> list = new List<customClass>(); list.Add(new customClass("Ann", 11000.5f, 12)); list.Add(new customClass("Ann", 1000.5f, 13)); list.Add(new customClass("Ann", 13000.5f, 11)); Debug.Log("未排序前:"); foreach (customClass item in list) { Debug.Log(item.toString()); } ascendingSortSalary(list); descendingSortSalary(list); ascendingSortLevel(list); descendingSortLevel(list); } /// <summary> /// salary 升序排列 /// </summary> /// <param name="list"></param> void ascendingSortSalary(List<customClass> list) { Debug.Log("salary 升序排列:"); list.Sort((x,y)=> { return x.salary.CompareTo(y.salary); }); foreach (customClass item in list) { Debug.Log(item.toString()); } } /// <summary> /// salary 降序排列 /// </summary> /// <param name="list"></param> void descendingSortSalary(List<customClass> list) { Debug.Log("salary 降序排列:"); list.Sort((x, y) => { return -x.salary.CompareTo(y.salary); }); foreach (customClass item in list) { Debug.Log(item.toString()); } } /// <summary> /// level 升序排列 /// </summary> /// <param name="list"></param> void ascendingSortLevel(List<customClass> list) { Debug.Log("level 升序排列:"); list.Sort((x, y) => { return x.level.CompareTo(y.level); }); foreach (customClass item in list) { Debug.Log(item.toString()); } } /// <summary> /// level 降序排列 /// </summary> /// <param name="list"></param> void descendingSortLevel(List<customClass> list) { Debug.Log("level 降序排列:"); list.Sort((x, y) => { return -x.level.CompareTo(y.level); }); foreach (customClass item in list) { Debug.Log(item.toString()); } } } 

 

 

小讯
上一篇 2025-03-09 07:20
下一篇 2025-03-22 21:10

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/133236.html