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

后端 未结 4 1392
时光取名叫无心
时光取名叫无心 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:47

    To setup the sort in your class:

    public static Comparison OtherComparison = delegate(MyClass object1, MyClass object2)
    {
        return object1.Whatever.CompareTo(object2.Whatever);
    };
    

    Then to sort using your new comparison:

    List myClassList = new List();
    myClassList.Sort(MyClass.OtherComparison);
    

    Except you clearly will not want to sort an empty list :)

提交回复
热议问题