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

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

相关推荐
星星法术嗲人11 分钟前
【Java】—— 集合框架:Collections工具类的使用
java·开发语言
黑不溜秋的25 分钟前
C++ 语言特性29 - 协程介绍
开发语言·c++
一丝晨光30 分钟前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
天上掉下来个程小白32 分钟前
Stream流的中间方法
java·开发语言·windows
xujinwei_gingko43 分钟前
JAVA基础面试题汇总(持续更新)
java·开发语言
sp_wxf1 小时前
Lambda表达式
开发语言·python
Fairy_sevenseven1 小时前
【二十八】【QT开发应用】模拟WPS Tab
开发语言·qt·wps
蜡笔小新星1 小时前
Python Kivy库学习路线
开发语言·网络·经验分享·python·学习
凯子坚持 c1 小时前
C语言复习概要(三)
c语言·开发语言
无限大.1 小时前
c语言200例 067
java·c语言·开发语言