C# Sort排序

C# Sort排序List的Sort方法排序有三种结果1,0,-1分别表示大于,等于,小于。1.对于数值类型的List(List),直接使用Sort进行排序。ListscoreList=newList(){89,100,78,23,67};scoreList.Sort();//默认按升序排列,相当于:scoreList.Sort((x,y)=>x.CompareTo(y))scoreLis

大家好,又见面了,我是你们的朋友全栈君。

List 的Sort方法排序有三种结果 1,0,-1分别表示大于,等于,小于。

1.对于数值类型的List (List<int>),直接使用Sort进行排序。

List<int> scoreList=new List<int>(){89,100,78,23,67};

scoreList.Sort();//默认按升序排列,相当于:scoreList.Sort((x,y)=>x.CompareTo(y))

scoreList.Sort((x,y)=>-x.CompareTo(y));//降序排列

2.对于非数值类型或者自定义类型,可通过实现IComparable接口重写CompareTo方法来排序:

public class Person : IComparable<Person>
    {
        public string Name { get; set; }
        public int Age { get; set; }

        //ComparetTo:大于 1; 等于 0; 小于 -1;
        public int CompareTo(Person p)
        {
            int result;
            if (this.Name == p.Name && this.Age == p.Age)
            {
                result = 0;
            }
            else
            {
                //this.Name表示后面的 Mary p.Name表示前面的 Bob
                //Mary 跟Bob 由小到大比较,如果Mary 与 Bob 比较 大于0(说明Mary 大于Bob),则 result=1(说明是由小到大的顺序)
                if (this.Name.CompareTo(p.Name) > 0)//先按名字小到大排列
                {
                    result = 1;
                }
                else if (this.Name == p.Name && this.Age > p.Age)//名字相同则按年龄由小到大排列
                {
                    result = 1;
                }
                else
                {
                    result = -1;
                }
            }
            return result;
        }

        public override string ToString()
        {
            return this.Name + "-" + this.Age;
        }
    }
          List<Person> lstPerson = new List<Person>();
            lstPerson.Add(new Person() { Name = "Bob", Age = 19 });
            lstPerson.Add(new Person() { Name = "Mary", Age = 18 });
            lstPerson.Add(new Person() { Name = "Mary", Age = 17 });
            lstPerson.Add(new Person() { Name = "Lily", Age = 20 });
            lstPerson.Sort();
            foreach (Person item in lstPerson)
            {
                Console.WriteLine(item.ToString());
            }
            Console.ReadKey();

输出:Bob-19 Lily-20 Mary-17 Mary-18

或不实现IComparable接口而使用linq排序:

  List<Person> lstPerson = new List<Person>();
            lstPerson.Add(new Person() { Name = "Bob", Age = 19 });
            lstPerson.Add(new Person() { Name = "Mary", Age = 18 });
            lstPerson.Add(new Person() { Name = "Mary", Age = 17 });
            lstPerson.Add(new Person() { Name = "Lily", Age = 20 });
            lstPerson.Sort();

            lstPerson.Sort((x, y) => {
                int result;
                if (x.Name == y.Name && x.Age == y.Age)
                {
                    result = 0;
                }
                else
                {
                    if (x.Name.CompareTo(y.Name) > 0)
                    {
                        result = 1;
                    }
                    else if (x.Name == y.Name && x.Age > y.Age)
                    {
                        result = 1;
                    }
                    else
                    {
                        result = -1;
                    }
                }
                return result;
            });


            foreach (Person item in lstPerson)
            {
                Console.WriteLine(item.ToString());
            }
            Console.ReadKey();

输出:Bob-19 Lily-20 Mary-17 Mary-18

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159784.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 源码大全_怎么复制网页源代码

    源码大全_怎么复制网页源代码7000个源码批量下载7000个源码批量下载<type=”text/javascript”language=”JavaScript”>document.title=”7000个源码批量下载-“+document.titlehttp://asp.lt263.com/soft/SaGuestBook.rar安全天使字符界面留言本(SaGuestBook)htt…

    2022年10月8日
    2
  • oracle恢复几天前的数据,恢复oracle数据到以前的某个时间点

    oracle恢复几天前的数据,恢复oracle数据到以前的某个时间点今天下午发现oracle数据库的参数表不知道被谁执行的语句都没有加条件,所以整个数据都乱了,不能用,查到了一下午,找到了几个解决办法,记录在此。一、执行如下SQL将test_temp表中的数据恢复到2014052811:00:00注意,这里一定要先删除全部数据,否则可能会导致数据重复deletefromtest_tmp;insertintotest_tmpselect*fro…

    2022年7月17日
    17
  • vue如何生成二维码_vue实现扫描二维码

    vue如何生成二维码_vue实现扫描二维码这里介绍两种vue生成二维码的方法QRcodevue-qrvue-qr比QRcode功能多在可以在中间加logo下面先介绍QRcodevue里安装qrcodejs的npm包npminstallqrcodejs2importQRCodefrom‘qrcodejs2’methods:{creatQrCode(){varqrcode=newQRCode(t…

    2022年10月3日
    3
  • Redis 使用lua脚本最全教程

    Redis 使用lua脚本最全教程1 redis 使用 lua 脚本的语法 RedisEval 命令 执行 Lua 脚本 redis127 0 0 1 6379 gt eval return KEYS 1 KEYS 2 ARGV 1 ARGV 2 2key1key2fir key1 2 key2 3 first 4 second 其中 script 参数是一段 Lua5 1 脚本程序 脚本不必 也不应该 定义为一个 Lua 函数 numkeys 用于指定键名

    2025年12月1日
    3
  • python 自带slic代码分析

    python 自带slic代码分析一 python 中的 slic 函数 defslic image n segments 100 compactness 10 max iter 10 sigma 0 spacing None multichannel True convert2lab None enforce connectivity True min size fact

    2025年7月21日
    4
  • oracle锁表_数据库锁表如何解决

    oracle锁表_数据库锁表如何解决1、执行以下语句可查询被锁的表selectb.owner,b.object_name,a.session_id,a.locked_modefromv$locked_objecta,dba_objectsbwhereb.object_id=a.object_id;2、执行以下语句可查询被锁的session和serialselectb.username,b.sid,b.serial#,logon_timefromv$locked_objecta,v$session.

    2022年8月23日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号