1

The below code always orders by the value of the enum, but I would like to do it by position in the enum. You noticed Matchup is third in the enum but with value of 6. It always comes out of the iteration as last in the list.

 foreach (var enumName in Enum.GetValues(enumType).Cast<TEnum>())
            {

}

 public enum RestrictionDetailType
    {
        [EnumMember(Value = "0"), Display(Name = "None")]
        None = 0,
        [EnumMember(Value = "1"), Display(Name = "Team")]
        Team = 1,
        [EnumMember(Value = "6"), Display(Name = "Matchup")]
        Matchup = 6,
        [EnumMember(Value = "2"), Display(Name = "Date/Time")]
        DateTime = 2,
        [EnumMember(Value = "3"), Display(Name = "Venue")]
        VenueCourt = 3,
        [EnumMember(Value = "4"), Display(Name = "Games")]
        Games = 4,
        [EnumMember(Value = "5"), Display(Name = "Exhibition")]
        Exhibition = 5
    }
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • 1
    You could make a list of an array of them in any order you want and use that instead. – juharr Oct 06 '17 at 14:20
  • 1
    Or, create a new Attribute to store display order and order by that in your enumeration. – Chris Pickford Oct 06 '17 at 14:21
  • 2
    @DiskJunky - eh? [`GetValues()`](https://msdn.microsoft.com/en-us/library/system.enum.getvalues(v=vs.110).aspx) is documented to return them ordered "by their unsigned magnitudes" – Damien_The_Unbeliever Oct 06 '17 at 14:31
  • 1
    The members of an enum are ordered by the indexes. That's how the enum knows how to order it's values. If you want to order them in a different way then you'd either have to assign indexes in the order you want, or add the values to a list or array in the order you want and then use that object instead. – JMadelaine Oct 06 '17 at 14:33
  • @Damien_The_Unbeliever, I stand corrected - good to know! – DiskJunky Oct 06 '17 at 14:41
  • It seems like you might want to see if there is a different solution to your problem that doesn’t require enum order, like using custom attributes or something, since what you want isn’t really possible. –  Oct 06 '17 at 17:20

2 Answers2

4
foreach(FieldInfo fi in typeof(RestrictionDetailType).GetFields()
  .Where(fi => fi.IsStatic).OrderBy(fi => fi.MetadataToken))
    Console.WriteLine(fi.Name);

Source: Sort enums in declaration order

MistyK
  • 6,055
  • 2
  • 42
  • 76
1

You could write your own method, like this:

public static List<T> GetEnumValuesInDeclarationOrder<T>()
{
    var t = typeof(T);
    // first type in this array is the data-type of the enum, int32 if not defined
    var members = t.GetFields();

    var result = new List<T>(members.Length - 1);
    foreach (FieldInfo mem in members.Skip(1))
        result.Add((T)mem.GetValue(null));

    return result;
}
Michael
  • 1,931
  • 2
  • 8
  • 22
  • 1
    [`GetFields`](https://msdn.microsoft.com/en-us/library/ch9714z3(v=vs.110).aspx): "The GetFields method does not return fields in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which fields are returned, because that order varies." – Damien_The_Unbeliever Oct 06 '17 at 14:33
  • @Damien_The_Unbeliever You're right. My fault. Then I think the only way would be adding another custom attribute to the enum-values and sort them by this custom-attribute values. – Michael Oct 06 '17 at 14:52