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

Posted by Gary Willoughby on Stack Overflow See other posts from Stack Overflow or by Gary Willoughby
Published on 2010-03-19T19:40:58Z Indexed on 2010/03/19 19:41 UTC
Read the original article Hit count: 218

Filed under:
|
|
|

I have a class that implements IComparable.

public class MyClass : IComparable<MyClass>
{
    public int CompareTo(MyClass c)
    {
        return this.whatever.CompareTo(c.whatever);
    }

    etc..
}

I then can call the sort method of a generic list of my class

List<MyClass> c = new List<MyClass>();
//Add stuff, etc.

c.Sort();

and have the list sorted according to my comparer.

How do i specify further comparers to sort my collection different ways according to the other properties of MyClass in order to let users sort my collection in a number of different ways?

© Stack Overflow or respective owner

Related posts about c#

Related posts about icomparable