博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSharp - Comparison between IComparer and IComparable
阅读量:7030 次
发布时间:2019-06-28

本文共 5011 字,大约阅读时间需要 16 分钟。

/*

Author: Jiangong SUN

*/

I've already written an article introducing the usage of comparer . 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
{ 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
        {            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
{ //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
{ 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
{ 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
() { 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
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!

references:

http://addinit.com/node/50

http://www.codeproject.com/Articles/42839/Sorting-Lists-using-IComparable-and-IComparer-Inte

 

你可能感兴趣的文章
代码管理(四)SVN和Git对比
查看>>
python - hadoop,mapreduce demo
查看>>
mongodb常见管理命令
查看>>
1.7 以函数对象取代函数
查看>>
Vue过渡效果之JS过渡
查看>>
Android项目实战(三):实现第一次进入软件的引导页
查看>>
Web Service基础——基础概念
查看>>
Linux2.4文件系统中vfsmount、安装点的dentry、设备的dentry之间的关系【转】
查看>>
POJ 1201 Intervals
查看>>
JAVA訪问URL
查看>>
APP接口基础学习一
查看>>
设计模式 策略模式 以角色游戏为背景
查看>>
【转】CSS和SVG中的剪切——clip-path属性和<clipPath>元素
查看>>
【C语言入门教程】5.4 递归
查看>>
UVALive 6915 Leveling Ground 倍增RMQ
查看>>
Inside ARC — to see the code inserted by the compiler
查看>>
云中气象 有备而来
查看>>
4.dubbo-demo+简易监控中心安装+管理控制台安装
查看>>
读书笔记《集体智慧编程》Chapter 4 : Searching and Ranking
查看>>
jquery form 插件 分类: JavaScript ...
查看>>