实现IComparable时CompareTo方法如何工作? [英] How does CompareTo method work while Implementing IComparable?

查看:62
本文介绍了实现IComparable时CompareTo方法如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂课

 public class Customer :IComparable<Customer>
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Salary { get; set; }
        }

我也有一个列表:

Customer cust = new Customer() { Id=10,Name="Jack",Salary= 15000};
            Customer cust1 = new Customer() { Id = 10, Name = "Abby", Salary = 5000 };
            Customer cust2 = new Customer() { Id = 10, Name = "Zed", Salary = 152000 };

            List<Customer> CustomerList = new List<Customer>();
            list.Add(cust);
            list.Add(cust1);
            list.Add(cust2);
            CustomerList.Sort();

我理解为什么list.Sort在Customer上不起作用,因为Customer类具有三个属性,并且它不知道如何对其进行排序.但是,如果我在 Customer 类中实现接口 IComparable ,则可以按我希望的方式对客户列表进行排序.

I understand why list.Sort wont work on Customer since Customer class has three properties and it does not know how to sort it. But if I implement the interface IComparable in Customer class I am able to sort the Customer List any way i want.

public class Customer :IComparable<Customer>
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Salary { get; set; }

            public int CompareTo(Customer other)
            {
                return this.Salary.CompareTo(other.Salary);
            }            
        }

现在我的问题是..如何实现CompareTo方法使我对 CustomerList 进行排序?我什至没有覆盖排序方法或其他任何东西.我很困惑,因为我根本没有使用CompareTo方法.

Now my question is.. How does implementing CompareTo method let me sort CustomerList? I am not even overriding Sort Method or anything. I am confused since I have not used CompareTo method at all.

我阅读了 https://stackoverflow.com/a/4188041/2064292 ,但它没有回答我的问题.

I read https://stackoverflow.com/a/4188041/2064292 but it does not answer my question.

推荐答案

当实现 IComparable< T> 时,创建一个 public int CompareTo(T)方法,您实际上要做的是告诉排序机制,该机制在其他地方定义,如何通过将一个实例与另一个实例进行比较来对您的类的两个实例进行排序.

When you implement IComparable<T>, creating a public int CompareTo(T) method, what you are essentially doing is telling the sorting mechanism, which is defined elsewhere, how to sort two instances of your class by comparing one instance to another.

就像我说的那样,实际的排序机制是在其他地方定义的,例如 List< T> .Sort() .无论排序机制使用哪种算法-气泡排序

Like I said, the actual sorting mechanism is defined elsewhere, e.g. List<T>.Sort(). Whatever algorithm the sorting mechanism uses -- bubble sort, quick sort, etc. -- is irrelevant, but they all need to compare instances against one another over and over to produce their sorted result. Each time two instances are compared, your public int CompareTo(T) method implementation is called because that's where you define how your specific implementations are compared to one another in order to produce the sorted result.

这篇关于实现IComparable时CompareTo方法如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆