如何在C#中排序枚举名称(升序或降序) [英] How to sort enum names in C# (Ascending or descending)

查看:685
本文介绍了如何在C#中排序枚举名称(升序或降序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



任何人都可以帮我排除枚举名称

例如:

  enum  V 
{
Kumar = 1
Raju = 2
Anil = 3
Suresh = 4
Bhaskar = 5
Chandra = < span class =code-digit> 6
};







i想要根据枚举名称将该枚举绑定到排序顺序列表(Kumar,Raju,Anil,..)

解决方案

没有自动在VS中按字母顺序对枚举进行排序的方法,以便:

  public  枚举 myEnum 
{
aaaa,
dddd,
eeee,
bbbb
}

变成

  public   enum  myEnum 
{
aaaa,
bbbb,
dddd,
eeee
}

如果这就是你想要的,那么你必须手动完成,或者使用外部文本编辑器(PSPad会非常容易地为你完成)。



如果还有其他类型你想做什么,然后看看: http://www.geospecialling.com/index.php/2013/02/enum-the-simple-c-value-type-we-love-to-complicate/ [ ^ ]解释了很多关于以不同的顺序显示枚举等等。


我认为你没有使用正确的对象来完成工作。 枚举中的元素顺序应由其保留的整数值控制,而不是由文本控制。图像在屏幕上按字母顺序显示的星期几枚举。



我认为您需要的是排序列表或字典。


有两种不同的东西:

1)绑定枚举,

2)排序数据。



广告1)

看看这里:

如何:绑定到枚举 [ ^ ]

访问枚举成员在Xaml [ ^ ]



Ad 2)

如果枚举绑定到任何控件,请使用其方法对数据进行排序。

HI,

Can any one help me out with sorting the enum names
for example:

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,..)

解决方案

There is no automatic way to sort enums into alphabetical order in VS so that:

public enum myEnum
    {
    aaaa,
    dddd,
    eeee,
    bbbb
    }

becomes

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.


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.


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.


这篇关于如何在C#中排序枚举名称(升序或降序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆