首页 > 编程语言 >【100个 Unity实用技能】| C# 中 Sort() 对List中的数据排序的几种方法 整理总结

【100个 Unity实用技能】| C# 中 Sort() 对List中的数据排序的几种方法 整理总结

时间:2022-11-04 23:03:22浏览次数:40  
标签:Sort name C# age List studentList Student new public


【100个 Unity实用技能】| C# 中 Sort() 对List中的数据排序的几种方法 整理总结_unity

Unity 小科普

老规矩,先介绍一下 Unity 的科普小知识:

  • Unity是 实时3D互动内容创作和运营平台 。
  • 包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助 Unity 将创意变成现实。
  • Unity 平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。
  • 也可以简单把 Unity 理解为一个游戏引擎,可以用来专业制作游戏!

【100个 Unity实用技能】| C# 中 Sort() 对List中的数据排序的几种方法 整理总结_排序小技巧_02


Unity 实用小技能学习

C#对List中的数据排序的几种方法

在C#中我们会经常用到List<>作为一个容器使用,在使用的过程中往往要对集合中的数据进行排序操作。

本文就来介绍一些好用的排序方法,一起来看看吧!

一、对 值类型 进行排序直接使用 Sort()方法

直接使用 C# 中的成员方法 Sort() 可以对C#本身的几种类型进行排序,比如 int,float,double 等。

Sort() 有四种重载如下:

public void Sort(Comparison<T> comparison);
public void Sort(int index, int count, IComparer<T> comparer);
public void Sort();
public void Sort(IComparer<T> comparer);

具体示例:

//申明一个List容器
List<int> list = new List<int>();
//向list中添加数据
list.Add(999);
list.Add(666);
list.Add(888);
//排序
list.Sort();

【100个 Unity实用技能】| C# 中 Sort() 对List中的数据排序的几种方法 整理总结_排序小技巧_03

值得一提的是,直接使用 Sort() 对List也可以排序,默认的排序规则是按照​​ASCII码​​进行的。

【100个 Unity实用技能】| C# 中 Sort() 对List中的数据排序的几种方法 整理总结_list_04

二、对自定义类型进行排序

首先声明一个自定义类型

class Student
{
public string name;
public int age;
public Student(string name,int age)
{
this.name = name;
this.age = age;
}
}

声明一个自定义类型的List

List<Student> studentList = new List<Student>();
studentList.Add(new Student("小Y",20));
studentList.Add(new Student("小小Y", 10));
studentList.Add(new Student("Y", 30));

//studentList.Sort();//会报错

此时直接使用studentList.sort()是报错的:ArgumentException:至少一个对象必须实现IComparable。

【100个 Unity实用技能】| C# 中 Sort() 对List中的数据排序的几种方法 整理总结_Sort_05


下面就来介绍几种可以自定义类型排序的几种方法

1. 继承接口IComparable<>

将自定义类型继承 接口IComparable<> ,并实现接口成员CompareTo

按照年龄进行排序,代码如下:

class Student:IComparable<Student>
{
public string name;
public int age;
public Student(string name,int age)
{
this.name = name;
this.age = age;
}
public int CompareTo(Student other)
{
//返回值含义:
//小于0:放在传入对象的前面
//等于0:保持当前的位置不变
//大于0:放在传入对象的后面
if (this.age > other.age) return 1;
else return -1;
}
}

此时声明一个自定义类型的List,并进行排序,就可以正常排序成功啦!

List<Student> studentList = new List<Student>();
studentList.Add(new Student("小Y",20));
studentList.Add(new Student("小小Y", 10));
studentList.Add(new Student("Y", 30));
studentList.Sort();

foreach (var l in studentList)
{
Debug.Log("name:"+l.name+",age:"+ l.age);
}

【100个 Unity实用技能】| C# 中 Sort() 对List中的数据排序的几种方法 整理总结_排序小技巧_06

2. 定义一个委托方法进行排序

Sort() 有一种重载参数是一个返回值为int类型的委托类型,可以在外面声明一个用来排序的方法。

代码如下:

class Student
{
public string name;
public int age;
public Student(string name,int age)
{
this.name = name;
this.age = age;
}
}

void Start()
{
List<Student> studentList = new List<Student>();
studentList.Add(new Student("小Y",20));
studentList.Add(new Student("小小Y", 10));
studentList.Add(new Student("Y", 30));
studentList.Sort(SortItem);//将方法名作为参数传递,实现排序
}

private int SortItem(Student stu1, Student stu2)
{
//传入的对象为列表中的对象
//进行两两比较,用左边的和右边的 按条件 比较
//返回值规则与接口方法相同
if (stu1.age > stu2.age) return 1;
else return -1;
}

上述代码也可以使用 ​​Lambda表达式​​ 直接排序,代码如下:

List<Student> studentList = new List<Student>();
studentList.Add(new Student("小Y",20));
studentList.Add(new Student("小小Y", 10));
studentList.Add(new Student("Y", 30));

studentList.Sort((stu1, stu2) => { return stu1.age > stu2.age ? 1 : -1; });

3.若自定义类型中有多个数值都要参与到排序规则中,可自定义排序类型先后

class Student
{
public string name;
public int age;
public int score;
public Student(string name,int age,int score)
{
this.name = name;
this.age = age;
this.score = score;
}

public static int Sort(Student a, Student b)
{
if (a.score > b.score)
{
// -1表示将a排在b前面
return -1;
}
else if (a.score == b.score)//若分数相同时,再按照年龄进行排序
{
if (a.age > b.age)
{
return -1;
}
else if (b.age > a.age)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 1;
}
}
}
void Start()
{
List<Student> studentList = new List<Student>();
studentList.Add(new Student("小Y", 20, 90));
studentList.Add(new Student("小小Y", 20, 80));
studentList.Add(new Student("Y", 30, 90));
studentList.Sort(Student.Sort);

foreach (var l in studentList)
{
Debug.Log("name:" + l.name + ",age:" + l.age + ",score:" + l.score);
}
}

【100个 Unity实用技能】| C# 中 Sort() 对List中的数据排序的几种方法 整理总结_unity_07

上述几种排序方法中 一般使用 委托方法的Lambda表达式 就可以满足我们大部分的排序需求了!

list.Sort((item1, item2) => { return item1.age > item2.age ? 1 : -1; });

【100个 Unity实用技能】| C# 中 Sort() 对List中的数据排序的几种方法 整理总结_c#_08


标签:Sort,name,C#,age,List,studentList,Student,new,public
From: https://blog.51cto.com/yyyy/5824885

相关文章