C# Sort排序

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



  • 11
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值