Sort a List by enum where enum is out of order

前端 未结 7 789
轻奢々
轻奢々 2020-12-25 11:15

I have a List of messages. Each message has a type.

public enum MessageType
{
    Foo = 0,
    Bar = 1,
    Boo = 2,
    Doo = 3
}

The enum

7条回答
  •  醉梦人生
    2020-12-25 11:55

    You may avoid writing a completely new type just to implement IComparable. Use the Comparer class instead:

    IComparer comparer = Comparer.Create((message) =>
        {
        // lambda that compares things
        });
    tempList.Sort(comparer);
    

提交回复
热议问题