当前位置: 移动技术网 > IT编程>开发语言>c# > C# 中的IComparable和IComparer

C# 中的IComparable和IComparer

2020年01月02日  | 移动技术网IT编程  | 我要评论

前言

在开发过程中经常会遇到比较排序的问题,比如说对集合数组的排序等情况,基本类型都提供了默认的比较算法,如string提供了按字母进行排序,而int整数则是根据整数大小进行排序.但是在引用类型中(具有多个字段),那么这个排序当然也是取决于我们特定的值。

icomparable接口

该接口由其值可以排序或排序的类型实现,并提供强类型的比较方法以对泛型集合对象的成员进行排序,例如数字可以大于第二个数字,一个字符串可以在另一个字符串之前以字母顺序出现。他要求实现类型定义的一个方法,compareto(t)该方法指示当前实现在排序顺序中的位置是在同一个类型和第二个对象之前、之后还是与其相同。通常,不会直接从开发人员代码中调用方法。相反他由list.sort()和add等方法自动调用。

通常,提供icomparable实现的类型还iequatable实现接口。iequatable接口equals定义方法,该方法确定实现类型的实例的相等性。

compareto(t)方法的实现必须int32返回具有以下三个值之一的,如下表所示。

含义
小于零 此对象在排序顺序中位于compareto方法所指定的对象之前。
此当前实例在排序顺序中与compareto方法参数指定的对象出现在同一位置。
大于零 此当前实例位于排序顺序中由compareto方法自变量指定的对象之后。

示例:

    class student : icomparable
    {
        public string name { get; set; }

        public int age { get; set; }
        public int compareto(object obj)
        {
            if (!(obj is student))
            {
                throw new argumentexception("compared object is not of student");
            }
            student student = obj as student;
            return age.compareto(student.age);
        }
    }

ps:我们根据通过age(int)来进行我们的排序

执行测试

class program
    {
        static void main(string[] args)
        {
            arraylist studentlist = new arraylist {
                new student{name="a",age=9 },
                  new student{name="a3",age=7 },
                 new student{name="a1",age=6 },
                 new student{name="a2",age=10 },
            };
            studentlist.sort();
            studentcomparable(studentlist);

            console.readline();
        }

        private static void studentcomparable(arraylist studentlist)
        {
            foreach (student item in studentlist)
            {
                console.writeline("name:{0},age:{1}", item.name, item.age);
            }
        }
    }

输出如下

icomparer接口

icomparable 接口的compareto方法一次只能对一个字段进行排序,因此无法对不同的属性进行排序。icomparer接口提供了compare方法,该方法比较两个对象并返回一个值,该值指示一个对象小于,等于或大于另一个对象。实现icomparer接口的类必须提供比较两个对象的compare方法。例如,您可以创建一个studentcomparer类,该类实现icomparer,并具有一个compare方法,该方法按name比较student对象。然后,您可以将studentcomparer对象传递给array.sort方法,它可以使用该对象对student对象的数组进行排序。

示例

    class studentcomparer : icomparer
    {

        public int compare(object x, object y)
        {
            student x1 = x as student;
            student y1 = y as student;
            return x1.name.compareto(y1.name);
        }
    }

ps:我们根据name(string)进行排序

执行测试

    class program
    {
        static void main(string[] args)
        {
            arraylist studentlist = new arraylist {
                new student{name="a",age=9 },
                  new student{name="a3",age=7 },
                 new student{name="a1",age=6 },
                 new student{name="a2",age=10 },
            };
            studentlist.sort(new studentcomparer());
            studentcomparable(studentlist);

            console.readline();
        }

        private static void studentcomparable(arraylist studentlist)
        {
            foreach (student item in studentlist)
            {
                console.writeline("name:{0},age:{1}", item.name, item.age);
            }
        }
    }

输出结果如下

icomparable和icomparer

上述示例中我们将对象进行了多次的装箱和拆箱,那么此时我们可以将方法改为泛型的,泛型的出现也让我们避免了装箱和拆箱的资源浪费.

最终我们实现的代码片段如下:

icomparable

    class student : icomparable<student>
    {
        public string name { get; set; }

        public int age { get; set; }

        public int compareto([allownull] student other)
        {
            return age.compareto(other.age);
        }
    }

icomparer

    class studentcomparer : icomparer<student>
    {

        public int compare([allownull] student x, [allownull] student y)
        {
            return x.name.compareto(y.name);
           
        }
    }

总结

参考:

示例:https://github.com/hueifeng/blogsample/tree/master/src/comparedemo

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网