Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
HI,

Can any one help me out with sorting the enum names
for example:
C#
enum V
    {
        Kumar = 1,
        Raju = 2,
        Anil = 3,
        Suresh = 4,
        Bhaskar = 5,
        Chandra = 6
    };



i want to bind that enum to list in sorting order based on Enum names(Kumar,Raju,Anil,..)
Posted
Updated 3-Mar-14 18:47pm
v3
Comments
Nelek 3-Mar-14 5:19am    
Don't think we can read minds or do astral projections to see your monitor. If you need help, the least you could do is to add some relevant code to your question or to explain your problem in such a way, that the users of CP can understand it. Otherwise, nobody will be able to help you.

Have a look:
What have you tried?[^]
How to ask a question?[^]

Please use the "improve question" and add relevant information or a piece of code envolved. There are thousands of ways to screw things up, how are we supposed to know which one did you choose?
dan!sh 3-Mar-14 5:19am    
Why would you want to do it?
sumanth n 3-Mar-14 5:33am    
Hi
i like to sort those enum names in Drop down list
BillWoodruff 4-Mar-14 0:45am    
What you want to do with the sorted enum names is important information that should be added to your question.

Yes, you can sort the values in an Enum for display in a ComboBox alpha-numerically, or any other way you want:
C#
// requires Linq

// preserve the current ComboBox selection
private V currentVName;

private void Form1_Load(object sender, EventArgs e)
{
    var vAry = Enum.GetValues(enumType: typeof (V)) as V[];

    // alpha-numeric sort
    comboBox1.DataSource = vAry.OrderBy(enm => enm.ToString()).ToArray();
}

// sample of how to get from the ComboBox selected Item to the Enum value
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    currentVName = (V) Enum.Parse(typeof(V), comboBox1.SelectedItem.ToString());
}
Of course a good question to ask is: if you create the Enum definition, why not just enter it in sorted order ?
 
Share this answer
 
v2
There is no automatic way to sort enums into alphabetical order in VS so that:
C#
public enum myEnum
    {
    aaaa,
    dddd,
    eeee,
    bbbb
    }
becomes
C#
public enum myEnum
    {
    aaaa,
    bbbb,
    dddd,
    eeee
    }
If that is what you want, you will have to do it manually, or use an external text editor (PSPad will do it for you pretty easily).

If there is some other sort you want to do, then look at this: http://www.geospecialling.com/index.php/2013/02/enum-the-simple-c-value-type-we-love-to-complicate/[^] which explains a lot about displaying enums in different order and so forth.
 
Share this answer
 
I don't think you are using right object for the job. Order of elements in an Enum should be governed by integer value it holds and not by the text. Image a days of week enum that is displayed alphabetically on screen.

I think what you need here is a sorted list or dictionary.
 
Share this answer
 
v2
There are two different things:
1) bind enumeration,
2) sort data.

Ad 1)
Have a look here:
How to: Bind to an Enumeration[^]
Accessing Enum members in Xaml[^]

Ad 2)
If enumeration is bind to any control, use its method to sort data.
 
Share this answer
 
Comments
sumanth n 3-Mar-14 8:09am    
Thanks for your valuable information but we are not using Xaml language in our code. Can you help me out without Xaml
i hope its useful for you.refer this

C# sort with examples
 
Share this answer
 

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