C#实现List.Sort()使用小计

 更新时间:2023年12月12日 09:36:52   作者:bughunter-  
在C#开发中,List是常见的一种集合类型,其提供了一个 Sort() 方法来实现对集合的排序,本文主要介绍了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();
}

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

到此这篇关于C#实现List.Sort()使用小计的文章就介绍到这了,更多相关C# List.Sort()内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#版Tesseract库的使用技巧

    C#版Tesseract库的使用技巧

    本文给大家分享C#版Tesseract库的使用技巧,在这里大家需要注意一下tesseract的识别语言包要自己下载后包含到项目里面,并设置为始终复制,或者直接把这个文件包放到运行程序目录(bin\debug)下的,具体实现代码跟随小编一起学习下吧
    2021-05-05
  • C#实现简单的登录界面

    C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。
    2015-11-11
  • WPF实现好看的Loading动画的示例代码

    WPF实现好看的Loading动画的示例代码

    这篇文章主要介绍了如何利用WPF实现好看的Loading动画效果,文中的示例代码讲解详细,对我们学习或工作有一定帮助,需要的可以参考一下
    2022-08-08
  • C#中Shear的用法实例

    C#中Shear的用法实例

    这篇文章主要介绍了C#中Shear的用法,实例分析了C#中使用Matrix实现Shear剪切变换的相关技巧,需要的朋友可以参考下
    2015-06-06
  • C#二叉搜索树算法实现步骤和实例代码

    C#二叉搜索树算法实现步骤和实例代码

    二叉搜索树(Binary Search Tree,简称BST)是一种节点有序排列的二叉树数据结构,这篇文章主要介绍了C#二叉搜索树算法实现步骤和实例代码,需要的朋友可以参考下
    2024-08-08
  • C#利用com操作excel释放进程的解决方法

    C#利用com操作excel释放进程的解决方法

    最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
    2013-03-03
  • C# WinForm实现检查目标IP端口是否可连接

    C# WinForm实现检查目标IP端口是否可连接

    这篇文章主要为大家详细介绍了如何基于C# WinForm实现检查目标IP端口是否可连接的小工具,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-01-01
  • c#基础——了解程序结构

    c#基础——了解程序结构

    这篇文章主要介绍了c# 程序结构的相关资料,文中讲解非常细致,帮助大家更好的理解和学习C#,感兴趣的朋友可以了解下
    2020-07-07
  • .Net Winform开发笔记(四)透过现象看本质

    .Net Winform开发笔记(四)透过现象看本质

    本文将从Winform应用程序中的Program.cs文件的第一行代码开始逐步分析一个Winform应用程序到底是怎样从出生走向死亡
    2013-01-01
  • C#常用日期时间方法汇总

    C#常用日期时间方法汇总

    这篇文章介绍了C#常用的日期时间方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04

最新评论