Sort a List by enum where enum is out of order

前端 未结 7 819
轻奢々
轻奢々 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 12:07

    No need to have the mapping. This should give you the list and order based on the enum. You don't have to modify anything even when you change the enum's order or and new items...

    var result = (from x in tempList
                  join y in Enum.GetValues(typeof(MessageType)).Cast()
                  on x equals y
                  orderby y
                  select y).ToList();
    

提交回复
热议问题