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

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

相关推荐
十五年专注C++开发20 分钟前
hiredis: 一个轻量级、高性能的 C 语言 Redis 客户端库
开发语言·数据库·c++·redis·缓存
WJ.Polar21 分钟前
Python数据容器-集合set
开发语言·python
晓131321 分钟前
JavaScript加强篇——第七章 浏览器对象与存储要点
开发语言·javascript·ecmascript
nbsaas-boot1 小时前
Go语言生态成熟度分析:为何Go还无法像Java那样实现注解式框架?
java·开发语言·golang
xiaocainiao8811 小时前
Python 实战:构建可扩展的命令行插件引擎
开发语言·python
碧海蓝天20221 小时前
C++法则21:避免将#include放在命名空间内部。
开发语言·c++
兮动人1 小时前
Java应用全链路故障排查实战指南:从系统资源到JVM深度诊断
java·开发语言·jvm
bianguanyue1 小时前
SQLite密码修改故障排查:RSA加密随机性导致的数据库匹配问题
数据库·sqlite·c#
R-sz2 小时前
导出word并且插入图片
开发语言·c#·word
CodeWithMe2 小时前
【读书笔记】《C++ Software Design》第一章《The Art of Software Design》
开发语言·c++