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即可。例如:

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,如下

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方法

代码:

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()时将其传入

代码:

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

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

相关推荐

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

    2023-12-11 18:36:02       35 阅读
  2. Redis(3)

    2023-12-11 18:36:02       28 阅读
  3. Flutter 1

    2023-12-11 18:36:02       26 阅读
  4. pprof火焰图排查问题

    2023-12-11 18:36:02       16 阅读
  5. C++随笔记录

    2023-12-11 18:36:02       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-11 18:36:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-11 18:36:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-11 18:36:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-11 18:36:02       20 阅读

热门阅读

  1. Go 反射技术判断结构体字段数据为空

    2023-12-11 18:36:02       42 阅读
  2. 面试经典150题(10-13)

    2023-12-11 18:36:02       35 阅读
  3. Android源码下载流程

    2023-12-11 18:36:02       46 阅读
  4. HBase

    HBase

    2023-12-11 18:36:02      34 阅读
  5. 手撕分布式缓存---互斥锁的优化

    2023-12-11 18:36:02       45 阅读
  6. 【CSP】202203-1_未初始化警告Python实现

    2023-12-11 18:36:02       35 阅读
  7. 【互联网小趣味】常用系统架构介绍扫盲

    2023-12-11 18:36:02       39 阅读
  8. mysql基本语法

    2023-12-11 18:36:02       39 阅读