Home > DeveloperSection > Forums > Sort enums in declaration order
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    August-14-2014 5:09 AM

 C# C# 
Ratings:


 1 Reply(s)
 741  View(s)
Rate this:

public enum CurrencyId

{

    USD = 840,

    UAH = 980,

    RUR = 643,

    EUR = 978,

    KZT = 398,

    UNSUPPORTED = 0

}

Is there any way to sort results of Enum.GetValues(typeof(CurrencyId)).Cast<CurrencyId>() by order they are declared in .cs file (USD, UAH, RUR, EUR, KZT, UNSUPPORTED), not by their underlying code? Personally, I believe the answer is 'no', because original order is lost in binaries, so... how can I implement the task?



Pravesh Singh

Total Post:411

Points:2881
Posted on    August-14-2014 6:52 AM

Hi Manoj, 
try this:

using System;

using System.Reflection;

public class Test

{

    public enum CurrencyId {

        USD = 840,

        UAH = 980,

        RUR = 643,

        EUR = 978,

        KZT = 398,

        UNSUPPORTED = 0

    }

    public static void Main()

    {

        foreach(FieldInfo fi in typeof(CurrencyId).GetFields())

            if(fi.IsStatic) Console.WriteLine(fi.Name);

    }

}

Output:

USD

UAH

RUR

EUR

KZT


Don't want to miss updates? Please click the below button!

Follow MindStick