【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 = "";

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

相关推荐
七七&5561 小时前
2024年08月13日 Go生态洞察:Go 1.23 发布与全面深度解读
开发语言·网络·golang
java坤坤1 小时前
GoLand 项目从 0 到 1:第八天 ——GORM 命名策略陷阱与 Go 项目启动慢问题攻坚
开发语言·后端·golang
元清加油1 小时前
【Golang】:函数和包
服务器·开发语言·网络·后端·网络协议·golang
健康平安的活着2 小时前
java之 junit4单元测试Mockito的使用
java·开发语言·单元测试
DjangoJason3 小时前
C++ 仿RabbitMQ实现消息队列项目
开发语言·c++·rabbitmq
m0_480502644 小时前
Rust 入门 KV存储HashMap (十七)
java·开发语言·rust
大阳1234 小时前
线程(基本概念和相关命令)
开发语言·数据结构·经验分享·算法·线程·学习经验
YA3334 小时前
java基础(九)sql基础及索引
java·开发语言·sql
奇树谦5 小时前
QT|windwos桌面端应用程序开发,当连接多个显示器的时候,如何获取屏幕编号?
开发语言·qt
weixin_307779135 小时前
VS Code配置MinGW64编译GNU 科学库 (GSL)
开发语言·c++·vscode·算法