C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法

list内元素的排序,相信大家都会遇到,怎么解决排序呢,我们一般使用 实现IComparable接口或者IComparer接口的方式来进行排序,下面我以一个简单程序为例子为大家讲解怎么进行排序。
我们先看看直接使用Sort排序list的状况

static void Main(string[] args)
        {
            Student student1 = new Student() { age = 25, name = "java", sex = "男" };
            Student student2 = new Student() { age = 22, name = "spng", sex = "男" };
            Student student3 = new Student() { age = 10, name = "c", sex = "男" };
            Student student4 = new Student() { age = 78, name = "key", sex = "男" };
            List<Student> list = new List<Student>() { student1, student2, student3, student4 };
            list.Sort();
            foreach(Student item in list)
            {
                Console.WriteLine(item.age);
            }
            Console.ReadKey();
        }
       class Student
        {
            public int age { get; set; }
            public string name { get; set; }
            public string sex { get; set; }
            public Student()
            {

            }
            public Student(int age, string name,string sex)
            {
                this.age = age;
                this.name = name;
                this.sex = sex;
            }
        }

运行上面的程序,可以看到程序报错了,是因为程序并不知道你要比较哪个元素,下来我们实现接口的方法来完成排序
这里写图片描述
这里写图片描述

1.IComparable接口

首先如果排序的条件只有一个时我使用IComparable接口进行排序。
1.实现IComparable接口
2.实现CompareTo方法
如下程序。

 class Student:IComparable<Student>
        {
            public int age { get; set; }
            public string name { get; set; }
            public string sex { get; set; }
            public Student()
            {

            }
            public Student(int age, string name,string sex)
            {
                this.age = age;
                this.name = name;
                this.sex = sex;
            }

            public int CompareTo(Student other)
            {
                return  this.age.CompareTo(other.age);
            }
        }

Student类实现类IComparable接口,并重写类CompareTo方法,
this.age.CompareTo(other.age)以年龄为排序条件进行升序

下来验证一下结果看有没有排序

  static void Main(string[] args)
        {
            Student student1 = new Student() { age = 25, name = "java", sex = "男" };
            Student student2 = new Student() { age = 22, name = "spng", sex = "男" };
            Student student3 = new Student() { age = 10, name = "c", sex = "男" };
            Student student4 = new Student() { age = 78, name = "key", sex = "男" };
            List<Student> list = new List<Student>() { student1, student2, student3, student4 };
            list.Sort();
            foreach(Student item in list)
            {
                Console.WriteLine(item.age);
            }
            Console.ReadKey();
        }

这里写图片描述

2.IComparer接口

1.创建新类实现IComparer接口
2.实现Compare方法
3.调用sort方法时进行重载

static void Main(string[] args)
        {
            Student student1 = new Student() { age = 25, name = "java", sex = "男" };
            Student student2 = new Student() { age = 22, name = "spng", sex = "男" };
            Student student3 = new Student() { age = 10, name = "c", sex = "男" };
            Student student4 = new Student() { age = 78, name = "key", sex = "男" };
            List<Student> list = new List<Student>() { student1, student2, student3, student4 };
            list.Sort(new AgeASC());
            foreach (Student item in list)
            {
                Console.WriteLine(item.age);
            }
            Console.ReadKey();
        }
       class Student
        {
            public int age { get; set; }
            public string name { get; set; }
            public string sex { get; set; }
            public Student()
            {

            }
            public Student(int age, string name,string sex)
            {
                this.age = age;
                this.name = name;
                this.sex = sex;
            }

            //public int CompareTo(Student other)
            //{
            //    return  this.age.CompareTo(other.age);
            //}
        }
        class AgeASC : IComparer<Student>
        {
            public int Compare(Student x, Student y)
            {
                return x.age.CompareTo(y.age);
            }
        }
        class AgeDESC : IComparer<Student>
        {
            public int Compare(Student x, Student y)
            {
                return y.age.CompareTo(x.age);
            }
        }

新类实现接口class AgeASC : IComparer
实现 public int Compare(Student x, Student y)方法
重载Sort方法 list.Sort(new AgeASC());

这里写图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Maybe_ch

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值