Robert J.
Robert J.

Reputation: 2711

c# sort collection based on enum

I would like to sort ObservableCollection<myClass> which, except for the other things, contains the enum property such as following:

public IP_Enums.ExportType ExportType { get; set; }

Where the structure of enum looks like following:

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

The problem I have is, that Export types under my ObservableCollection<myClass> are not in an order (as you can see in the enum definition), but like this e.g.:

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

I would like to sort my collection based on this ExportType, but I am not quite sure how to do it (as I have [A-Z] character followed by digit [0-9].

Any help would be highly appreciated.

Upvotes: 1

Views: 219

Answers (1)

Dmitry
Dmitry

Reputation: 14059

You can create a new ObservableCollection<myClass> collection sorted by the ExportType values:

ObservableCollection<myClass> collection = ...;
collection = new ObservableCollection<MyClass>(collection.OrderBy(item => item.ExportType));

Upvotes: 2

Related Questions