C# - How to implement multiple comparers for an IComparable class?

后端 未结 4 1359
时光取名叫无心
时光取名叫无心 2020-12-30 08:22

I have a class that implements IComparable.

public class MyClass : IComparable
{
    public int CompareTo(MyClass c)
    {
        return this         


        
4条回答
  •  一生所求
    2020-12-30 08:43

    One step in that direction would be to use the Sort overload that let's you specify an IComparer (not an IComparable, though).

    If you already have a lot of IComparable implementations, it should be trivial to write a general-purpose implementation of IComparer that compares two IComparable instances. In fact, I'm a bit surprised such a class doesn't already exist in the BCL, but I haven't been able to find one.

    Comparer.Default comes close, but not quite.

提交回复
热议问题