当前位置:编程学习 > C#/ASP.NET >>

CSharp - Comparison between IComparer and IComparable

/*

Author: Jiangong SUN

*/

 


I've already written an article introducing the usage of comparer here. In this article I'll compare the usage of IComparable and IComparer with examples.

 


Important difference:  A class can have only one Comparable, but multiple Comparer.

 


Here I have a class Student with different properties, which will be used as sorting criterias.


        public class Student : IComparable<Student>
        {
            public string LastName { get; set; }
            public string FirstName { get; set; }
            public int Age { get; set; }
            public int Class { get; set; }
        }


Firstly, I want class Student to implement interface IComparable, and I must override method CompareTo of interface IComparable. In this method I sort Student list in an ascending order.


        //class who implement interface IComparable, must implement CompareTo method
        public class Student : IComparable<Student>
        {
            public string LastName { get; set; }
            public string FirstName { get; set; }
            public int Age { get; set; }
            public int Class { get; set; }
            //CompareTo method compare itself to another object
            public int CompareTo(Student student)
            {
                return LastName.CompareTo(student.LastName);
            }
        }In this way, Student list will be sorted by LastName in an ascending order.

 


Next, I'll create 3 comparers who will use the rest 3 properties as sorting criteria.

 


        //Comparer 1
        //class who implement interfce IComparer, must implement Compare method
        public class OrderByAgeAscending : IComparer<Student>
        {
            //Compare method compares two objects
            public int Compare(Student x, Student y)
            {
                if (x.Age.CompareTo(y.Age) < 0)
                {
                    return -1;
                }
                if (x.Age.CompareTo(y.Age) == 0)
                {
                    return 0;
                }
                return 1;
            }
        }

        //Comparer 2
        public class OrderByClassDescending : IComparer<Student>
        {
            public int Compare(Student x, Student y)
            {
                if (x.Class.CompareTo(y.Class) < 0)
                {
                    return 1;
                }
                if (x.Class.CompareTo(y.Class) == 0)
                {
                    return 0;
                }
                return -1;
            }
        }

        //Comparer 3
        public class OrderByFirstNameAscending : IComparer<Student>
        {
            public int Compare(Student x, Student y)
            {
                if (x.FirstName.CompareTo(y.FirstName) < 0)
                {
                    return -1;
                } if (x.FirstName.CompareTo(y.FirstName) == 0)
                {
                    return 0;
                }
                return 1;
            }
        }
Now I will create a new Student list with some Students and try to sort them with comparable and comparer.

Firstly, I will create a list.


            var s1 = new Student() { LastName = "charles", FirstName = "

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,