C#随笔 | List.Sort()使用小计

1、使用List.Sort()对基本数值类型数据进行排序

案例:对存放int数据的List进行排序

其实C#中的List的Sort函数中的比较函数CompareTo有三种结果 1, -1 ,0分别代表大,小,相等。默认List的排序是升序排序。

举个例子:在比较函数CompareTo()中,如果 x>y return 1;则是按照升序排列。如果x>y return -1;则是按照降序排列。这就是1和-1大小的含义。其实你非要这么写 x<y return 1;则也表示降序排列。不过大家一般习惯x>y return 1;升序,如果想要降序只需return -1;即可。

Tips:系统List默认的排序是升序,如果你想要降序,可以直接在比较函数前面加个负号,把返回结果由1变成-1即可。例如:

csharp 复制代码
List<int> list = new List<int>() { 2, 1, 3, 4, 5 };
list.Sort((x, y) => -x.CompareTo(y));	
for (int i = 0; i < list.Count(); i++)
{
    Console.Write(list[i] + " ");
}
Console.WriteLine();

2、使用List.Sort()对非数值类型、string、结构体、对象等或者官方未实现IComparable接口的类型进行排序

案例:

假设我们目前有一个Person类和存放Person类数据的List,如下

csharp 复制代码
public class Person
{
    public int id;
    public string name;

    public Person()
    {
        id = 0;
        name = "name";
    }
    public Person(int id_, string name_)
    {
        id = id_;
        name = name_;
    }
 }


static void Main(string[] args)
{
    List<Person> list2 = new List<Person>();
    list2.Add(new Person(10001, "Tom"));
    list2.Add(new Person(10003, "Jerry"));
    list2.Add(new Person(10002, "Ben"));
    list2.Add(new Person(10005, "Cat"));
    list2.Add(new Person(10004, "Danny"));
}

假设我们现在的需求是将这个其按照id从小到大进行排序。

我们有两种办法:

方法一:实现IComparable接口重写CompareTo方法

代码:

csharp 复制代码
public class Person : IComparable<Person>
{
    public int id;
    public string name;

    public Person()
    {
        id = 0;
        name = "name";
    }
    public Person(int id_, string name_)
    {
        id = id_;
        name = name_;
    }

    public int CompareTo(Person obj_)
    {
        if (this.id > obj_.id)
            return 1;
        else
            return -1;
    }
}

static void Main(string[] args)
{
    List<int> list = new List<int>() { 2, 1, 3, 4, 5 };
    list.Sort((x, y) => -x.CompareTo(y));
    for (int i = 0; i < list.Count(); i++)
    {
        Console.Write(list[i] + " ");
    }
    Console.WriteLine();
    Console.WriteLine();

    List<Person> list2 = new List<Person>();
    list2.Add(new Person(10001, "Tom"));
    list2.Add(new Person(10003, "Jerry"));
    list2.Add(new Person(10002, "Ben"));
    list2.Add(new Person(10005, "Cat"));
    list2.Add(new Person(10004, "Danny"));
    // List.Sort自定义对象结构体排序规则的第一种办法:在类中实现IComparable接口
    list2.Sort();
    for (int i = 0; i < list2.Count(); i++)
    {
        Console.WriteLine(list2[i].id + " " + list2[i].name);
    }
    Console.WriteLine();
}

方法二:编写一个排序规则的函数,调用LIst.Sort()时将其传入

代码:

csharp 复制代码
public class Person
{
     public int id;
     public string name;

     public Person()
     {
         id = 0;
         name = "name";
     }
     public Person(int id_, string name_)
     {
         id = id_;
         name = name_;
     }
}

private static int CmpFun(Person a, Person b)
{
    if (a.id > b.id)
        return 1;
    else
        return -1;
}


static void Main(string[] args)
{
    List<Person> list2 = new List<Person>();
    list2.Add(new Person(10001, "Tom"));
    list2.Add(new Person(10003, "Jerry"));
    list2.Add(new Person(10002, "Ben"));
    list2.Add(new Person(10005, "Cat"));
    list2.Add(new Person(10004, "Danny"));
	
	// List.Sort自定义对象结构体排序规则的第二种办法:写一个排序的规则函数,将其传入
    list2.Sort(CmpFun);
    for (int i = 0; i < list2.Count(); i++)
    {
        Console.WriteLine(list2[i].id + " " + list2[i].name);
    }
    Console.WriteLine();
}

请注意,在不带括号的情况下使用方法名称。 将方法用作参数会告知编译器将方法引用转换为可以用作委托调用目标的引用,并将该方法作为调用目标进行附加。

相关推荐
c#上位机3 小时前
wpf之ToggleButton控件
c#·wpf
mit6.82413 小时前
[C# starter-kit] 命令/查询职责分离CQRS | MediatR |
java·数据库·c#
beyond谚语15 小时前
C#学习小笔记(完整版)—— Patience
c#
爱吃小胖橘15 小时前
Unity网络开发--超文本传输协议Http(1)
开发语言·网络·网络协议·http·c#·游戏引擎
IT小农工16 小时前
Word 为每一页设置不同页边距(VBA 宏)
开发语言·c#·word
sali-tec16 小时前
C# 基于halcon的视觉工作流-章42-手动识别文本
开发语言·人工智能·算法·计算机视觉·c#·ocr
咕白m62518 小时前
C# 合并多个PDF文档:高效解决方案
c#·.net
c#上位机20 小时前
wpf之MVVM中只读属性更新界面
c#·wpf·mvvm
技术支持者python,php1 天前
winform本地上位机-ModbusRTC1.上位机控制台与数据监控(数据监控架构思维与图表系列)
c#
kalvin_y_liu1 天前
DeploySharp开源发布:让C#部署深度学习模型更加简单
深度学习·开源·c#