Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi .
how i can Order enum members by their values ?
for example i have this enum :
C#
public static enum ePersianNumbersliteral
   {
       Zero = "\u0660",
       One = "\u0661",
       Two = "\u0662",
       Three = "\u0663",
       Four = "\u0664",
       Five = "\u0665",
       Six = "\u0666",
       Seven = "\u0667",
       Eight = "\u0668",
       Nine = "\u0669"
   }

now when i use this type members are unordered . Indeed ordered by alphabetic!
Eight
...
..
.
i want order members by value!
Posted
Comments
Andreas Gieriet 21-Feb-12 2:44am    
Please give an example where it is evident what you mean by "use this type".
An enum type does not get magically sorted in any way.
Saeid.Babaei86 21-Feb-12 2:58am    
Hi for example i want to use this code

ePersianNumbersliteral one=ePersianNumbersliteral.One ;

if i use this code the enum member are unsorted . and Eigh is a first member !
Andreas Gieriet 21-Feb-12 3:08am    
This example does not show any sorting, yu simply assign a value...(?)

See Solution #2 for sorting of enums.
Andreas Gieriet 21-Feb-12 3:15am    
Or are you talking about intellisense? I.e. when you select an enum *while editing* intellisense sorts them alphabetically? I don't know of any easy way to change that behaviour of the editor/intellisense.
Saeid.Babaei86 21-Feb-12 3:32am    
Oh yes yes intellisense

If you *really* want intellisense to list the enums in a value sequence, that value has to be visible in the name as well so that intellisense sorts them properly by sorting the values alphabetically. E.g.

C#
public enum ePersianNumbersliteral
{
    E0_Zero = '\u0660',
    E1_One = '\u0661',
    E2_Two = '\u0662',
    E3_Three = '\u0663',
    E4_Four = '\u0664',
    E5_Five = '\u0665',
    E6_Six = '\u0666',
    E7_Seven = '\u0667',
    E8_Eight = '\u0668',
    E9_Nine = '\u0669'
}


Not that I like this. I never had the whish to have intellisense sort the enums by value, though... My enums in general hide the value they stand for and therefore, the sorting is kind of arbitrary - why not having on top the most often used one? ;-)

This is a solution to trick intellisense when it tricks you ;-)
 
Share this answer
 
Comments
Saeid.Babaei86 21-Feb-12 3:34am    
Veryy thanks . i think this is the best way . really thanks!
Try this:

C#
public enum ePersianNumbersliteral
{
    Zero = '\u0660',
    One = '\u0661',
    Two = '\u0662',
    Three = '\u0663',
    Four = '\u0664',
    Five = '\u0665',
    Six = '\u0666',
    Seven = '\u0667',
    Eight = '\u0668',
    Nine = '\u0669'
}


C#
public static IEnumerable<T> SortEnumByValue<T>()
{
    return from e in Enum.GetValues(typeof(T)).Cast<T>()
           orderby e
           select e;
}
public static IEnumerable<T> SortEnumByName<T>()
{
    return from e in Enum.GetValues(typeof(T)).Cast<T>()
           let nm = e.ToString()
           orderby nm
           select e;
}


C#
Console.WriteLine("{0}",
     string.Join(", ", SortEnumByValue<ePersianNumbersliteral>()));
Console.WriteLine("{0}",
     string.Join(", ", SortEnumByName<ePersianNumbersliteral>()));


The output is:
Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine
Eight, Five, Four, Nine, One, Seven, Six, Three, Two, Zero
 
Share this answer
 
v2
This article sorts an enum - Fill Combobox With Sorted Enum Without Code[^].
 
Share this answer
 
Comments
Saeid.Babaei86 21-Feb-12 2:59am    
Hi . thanks but i dont want fill any control ! . i just want order my enum . not any way ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900