1

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.

5
  • what's the expected output? Commented Sep 27, 2015 at 17:21
  • I want to order the same collection, as I am afterwards flushing all elements into a XML file. Commented Sep 27, 2015 at 17:22
  • yourCollection.Orderby(x => x.ExportType); is that what you want? Commented Sep 27, 2015 at 17:23
  • I have already tried myCollection.OrderBy() but it did not order collection by any means Commented Sep 27, 2015 at 17:24
  • @stuartd I am not trying to change enum order, I am trying to order all items under my collection in an ascending order - from A1 to D2 Commented Sep 27, 2015 at 17:25

1 Answer 1

2

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));
Sign up to request clarification or add additional context in comments.

3 Comments

It's strange, but when I create new collection (not trying to order already existing collection) this suddenly works. Could you please explain why is this?
Does myClass implement IComparable<myClass>?
No, it implements IEquatable<myClass>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.