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();
}

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

相关推荐
LZQqqqqo5 小时前
C# 中生成随机数的常用方法
java·算法·c#
weixin_4569042710 小时前
C#泛型委托讲解
开发语言·c#
三千道应用题12 小时前
C#模式匹配用法与总结
c#
深海潜水员13 小时前
【Unity】背包系统 + 物品管理窗口 (上)
开发语言·vscode·游戏·unity·c#·游戏引擎
伽蓝_游戏13 小时前
Unity UI的未来之路:从UGUI到UI Toolkit的架构演进与特性剖析(6)
游戏·ui·unity·架构·c#·游戏引擎·.net
kyranhan14 小时前
C#程序本地运行正常,通过网络下载报错:FileLoadException:“未能加载文件或程序集“xxx.dll”或它的某一个依赖项。
开发语言·c#·wpf
Yueeyuee_14 小时前
【C#学习Day14笔记】泛型、集合(数组列表Arraylist、列表list)与字典
笔记·学习·c#
呆瑜nuage14 小时前
list的使用和模拟
c++·list
隐-梵15 小时前
2025年测绘程序设计比赛--基于统计滤波的点云去噪(已获国特)
java·开发语言·windows·c#·.net
R-G-B16 小时前
【09】大恒相机SDK C#开发 ——库函数 IntPtr ConvertToRGB24详细解释 及 示例
c#·大恒相机sdk·converttorgb24