【C#】 List.Sort 方法

【C#】 List.Sort 方法

在C#中,List.Sort()不仅为系统自带的变量(int, float, double ...)类型的集合提供默认排序,还提供了自定义的排序方法。

List自带排序

csharp 复制代码
List<int> list = new List<int>();
list.Add(5);
list.Add(3);
list.Add(4);
list.Add(6);
list.Add(2);
list.Add(1);

string str = "";
for(int i = 0; i < list.Count; i++)
{
    str += list[i].ToString() + " ";
}
Console.WriteLine(str);//输出:5 3 4 6 2 1
str = "";
Console.WriteLine("--------------------------");
list.Sort();//排序
for (int i = 0; i < list.Count; i++)
{
    str += list[i].ToString() + " ";
}
Console.WriteLine(str);//输出:1 2 3 4 5 6
str = "";

List自定义排序方式

1.继承接口 IComparable,实现CompareTo()方法

csharp 复制代码
class Item : IComparable<Item>
{
    private int m_atk;
    public int Atk
    {
        get
        {
            return m_atk;
        }
    }
    public Item(int atk)
    {
        m_atk = atk;
    }

    public int CompareTo(Item other)
    {
        //传入的对象相当于0的位置
        //other = 传入对象
        //小于0:
        //放在传入对象的前面
        //等于0:
        //保持当前的位置不变
        //大于0:
        //放在传入对象的后面
        if (this.Atk > other.Atk)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
}

将此实例与CompareTo传入的对象进行比较,并指示此实例在排序顺序中时位于传入对象之前、之后还是同一位置。

"值" 条件
<0 此实例位于 other 之前
=0 此实例在排序顺序中的位置与 other 相同
>0 此实例位于 other 之后
csharp 复制代码
List<Item> itemList = new List<Item>();
itemList.Add(new Item(4));
itemList.Add(new Item(1));
itemList.Add(new Item(3));
itemList.Add(new Item(2));
itemList.Add(new Item(5));

itemList.Sort();//Item类必须继承IComparable接口实现方法,否则报错
for (int i = 0; i < itemList.Count; i++)
{
    str += itemList[i].Atk.ToString() + " ";
}
Console.WriteLine(str);//输出:1 2 3 4 5
str = "";

2.在Sort中传入 Comparison 委托函数,自定义比较方法

csharp 复制代码
class Student
{
    private int m_stuId;
    public int StuId
    {
        get
        {
            return m_stuId;
        }
    }

    public Student(int stuId)
    {
        m_stuId = stuId;
    }
}
csharp 复制代码
List<Student> studentList = new List<Student>();
studentList.Add(new Student(20238802));
studentList.Add(new Student(20238803));
studentList.Add(new Student(20238804));
studentList.Add(new Student(20238805));
studentList.Add(new Student(20238801));
//studentList.Sort(SortStudent);

studentList.Sort((stu1, stu2) =>
{
    if (stu1.StuId > stu2.StuId)
    {
        return 1;
    }
    else
    {
        return -1;
    }
});

for (int i = 0; i < studentList.Count; i++)
{
    str += studentList[i].StuId.ToString() + " ";
}
Console.WriteLine(str);//输出:20238801 20238802 20238803 20238804 20238805
str = "";

因为作者精力有限,文章中难免出现一些错漏,敬请广大专家和网友批评、指正。

相关推荐
向阳@向远方15 分钟前
第二章 简单程序设计
开发语言·c++·算法
Mr_Xuhhh1 小时前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构
纳兰青华1 小时前
bean注入的过程中,Property of ‘java.util.ArrayList‘ type cannot be injected by ‘List‘
java·开发语言·spring·list
好开心啊没烦恼1 小时前
Python 数据分析:DataFrame,生成,用字典创建 DataFrame ,键值对数量不一样怎么办?
开发语言·python·数据挖掘·数据分析
liulilittle1 小时前
VGW 虚拟网关用户手册 (PPP PRIVATE NETWORK 基础设施)
开发语言·网络·c++·网关·智能路由器·路由器·通信
Devil枫1 小时前
Kotlin高级特性深度解析
android·开发语言·kotlin
ChinaDragonDreamer1 小时前
Kotlin:2.1.20 的新特性
android·开发语言·kotlin
安之若素^2 小时前
启用不安全的HTTP方法
java·开发语言
一个天蝎座 白勺 程序猿2 小时前
Python(28)Python循环语句指南:从语法糖到CPython字节码的底层探秘
开发语言·python
持梦远方2 小时前
C 语言基础入门:基本数据类型与运算符详解
c语言·开发语言·c++