【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++开发3 分钟前
设计模式之单例模式(二): 心得体会
开发语言·c++·单例模式·设计模式
flyair_China30 分钟前
【云架构】
开发语言·php
Kookoos35 分钟前
性能剖析:在 ABP 框架中集成 MiniProfiler 实现性能可视化诊断
后端·c#·.net·abp vnext·miniprofiler
Chef_Chen36 分钟前
从0开始学习R语言--Day20-ARIMA与格兰杰因果检验
开发语言·学习·r语言
zh_xuan37 分钟前
c++ std::pair
开发语言·c++
CodeWithMe1 小时前
【C/C++】EBO空基类优化介绍
开发语言·c++
404.Not Found1 小时前
Day46 Python打卡训练营
开发语言·python
love530love1 小时前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm
凌辰揽月1 小时前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
海奥华21 小时前
go中的接口返回设计思想
开发语言·后端·golang