ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#比较器Comparer/Sort排序的类内实现和类外实现

2022-05-31 01:03:20  阅读:128  来源: 互联网

标签:Sort return 类外 C# age list int null public


比较规则

CompareTo(Object)

将此实例与指定对象进行比较并返回一个对二者的相对值的指示。

public int CompareTo (object? value);

参数value Object要比较的对象,或为 null。

返回Int32一个带符号数字,指示此实例和 value 的相对值。

返回值

说明

小于零

此实例小于 value。

此实例等于 value。

大于零

此实例大于 value,或 value 为 null。

类内实现比较器

//Person类,自带比较器
    public class Person : IEquatable<Person>, IComparable<Person>
    {
        public int age;
        public string name;
        public Person(int age)
        {
            this.age = age;
        }
        public Person(string name, int age) : this(age)
        {
            this.name = name;
        }
        public override string ToString()
        {
            return $"姓名:{this.name} + 年龄: {this.age}";
        }
        //实现可比较接口
        public int CompareTo(Person other)
        {
            if (other == null) return 1;
            return this.age.CompareTo(other.age);
        }
        //实现可相等接口
        public bool Equals(Person other)
        {
            if (other == null) return false;
            return this.age.Equals(other.age);
        }
        //运算符重载
        public static bool operator >(Person person, Person other)
        { if (other == null) return true; return person.age > other.age; }
        public static bool operator >=(Person person, Person other)
        { if (other == null) return true; return person.age >= other.age; }
        public static bool operator <(Person person, Person other)
        { if (other == null) return false; return person.age < other.age; }
        public static bool operator <=(Person person, Person other)
        { if (other == null) return false; return person.age <= other.age; }


    }

测试

static void test1()
        {
            List<Person> list = new List<Person>();
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            { list.Add(new Person($"Tom{i}", r.Next(1, 100))); }
            foreach (var item in list)
            { Console.WriteLine(item.ToString()); }
            //Person自带比较器
            list.Sort();
            Console.WriteLine("排序后");
            foreach (var item in list)
            { Console.WriteLine(item.ToString()); }

        }

 运行结果

 

 类外实现

//学生类,无比较器,需要在外部专门声明一个比较器
    public class Student
    {
        public int age;
        public string name;
        public Student(int age)
        {
            this.age = age;
        }
        public Student(string name, int age) : this(age)
        {
            this.name = name;
        }
        public override string ToString()
        {
            return $"姓名:{this.name} + 年龄: {this.age}";
        }
    }
    //学生比较器,实现可比较接口(默认)
    public class StudentComparerAscend : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            int result;
            if(x==null&&y==null ) result= 0;
            if (x == null && y != null) result = -1;
            if (x != null && y == null) result = 1;
            result= x.age.CompareTo(y.age);
            return result;
        }
    }
    //学生比较器,实现可比较接口(反序)
    public class StudentComparerDescend : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            int result;
            if (x == null && y == null) result = 0;
            if (x == null && y != null) result = -1;
            if (x != null && y == null) result = 1;
            result = x.age.CompareTo(y.age);
            return -result;//实现反序
        }
    }

测试

       static void test2()
        {
            List<Student> list = new List<Student>();
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            { list.Add(new Student($"Jack{i}", r.Next(1, 100))); }
            foreach (var item in list)
            { Console.WriteLine(item.ToString()); }
            //实例化升序比较器
            StudentComparerAscend comparerAscend = new StudentComparerAscend();
            //升序排序(默认)
            list.Sort(comparerAscend);
            Console.WriteLine("\n升序排序后");
            foreach (var item in list)
            { Console.WriteLine(item.ToString()); }
            //实例化降序比较器
            StudentComparerDescend comparerDescend = new StudentComparerDescend();
            //降序排序
            list.Sort(comparerDescend);
            Console.WriteLine("\n降序排序后");
            foreach (var item in list)
            { Console.WriteLine(item.ToString()); }
        }

运行结果

 

标签:Sort,return,类外,C#,age,list,int,null,public
来源: https://www.cnblogs.com/zhangdezhang/p/16328929.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有