程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> CSharp - Comparison between IComparer and IComparable

CSharp - Comparison between IComparer and IComparable

編輯:C#入門知識

/*

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 = "charles", Age = 27, Class = 15 };
            var s2 = new Student() { LastName = "viz", FirstName = "newton", Age = 20, Class = 30 };
            var s3 = new Student() { LastName = "la", FirstName = "aba", Age = 2, Class = 2 };
            var students = new List<Student>() { s1, s2, s3 };
Then, I will sort the list with comparable. I just need to call Sort() method, it will use the CompareTo() method I've created.


            //IComparable: sort by last name
            students.Sort();
            Display(students, "IComparable OrderByLastName:");

 

And then, I will use the 3 comparers to sort the list.

            //Comparer 1: order by age
            var c1 = new OrderByAgeAscending();
            students.Sort(c1);
            Display(students, "Comparer OrderByAgeAscending:");

            //Comparer 2: order by class
            var c2 = new OrderByClassDescending();
            students.Sort(c2);
            Display(students, "Comparer OrderByClassDescending:");

            //Comparer 3: order by first name
            var c3 = new OrderByFirstNameAscending();
            students.Sort(c3);
            Display(students, "Comparer OrderByFirstNameAscending");
And there are an Display() method who is in charge of display the student information.


        public static void Display(List<Student> students, string comparerName)
        {
            Console.WriteLine(comparerName);
            foreach (var student in students)
            {
                Console.WriteLine("last name: " + student.LastName + "; first name: " + student.FirstName + "; age: " + student.Age + "; class: " + student.Class);
            }
        }
Now, let's see the results:

 

 \
 

Right now, we are arrived at the end of the article. I hope you can find useful information here. Enjoy coding!

 

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved