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

这里写图片描述

猜你喜欢

转载自blog.csdn.net/Maybe_ch/article/details/81359408
今日推荐