c#基于枚举的排序集合

时间:2015-09-27 17:17:59

标签: c# sorting enums

我想对ObservableCollection<myClass>进行排序,除了其他内容之外,还包含enum属性,如下所示:

public IP_Enums.ExportType ExportType { get; set; }

enum的结构如下所示:

    public enum ExportType
    {
        A1,
        A2,
        B1,
        B2,
        B3,
        C1,
        C2,
        D1,
        D2
    }

我遇到的问题是,ObservableCollection<myClass>下的导出类型不在顺序中(正如您在枚举定义中看到的那样),但是像这样:。

[0].ExportType = ExportType.B2
[1].ExportType = ExportType.A1
[2].ExportType = ExportType.D2
[3].ExportType = ExportType.A1

我想根据这个ExportType对我的集合进行排序,但我不太清楚该怎么做(因为我有[A-Z]字符后跟数字[0-9]。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

您可以创建按ObservableCollection<myClass>值排序的新ExportType集合:

ObservableCollection<myClass> collection = ...;
collection = new ObservableCollection<MyClass>(collection.OrderBy(item => item.ExportType));
相关问题