Click here to Skip to main content
11,932,402 members (55,948 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Enum
Hi .
how i can Order enum members by their values ?
for example i have this enum :
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!
i want order members by value!
Posted 20-Feb-12 21:27pm
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
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This article sorts an enum - Fill Combobox With Sorted Enum Without Code[^].
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 ?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Try this:

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

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;

     string.Join(", ", SortEnumByValue<ePersianNumbersliteral>()));
     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
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.

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? Wink | ;-)

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 21 Feb 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100