0

I have an array and one of its elements uses enum. I want to sort the array by enum.

public enum EnumList
    {
        Spring,
        Summer,
        Fall,
        Winter,
        Unknown
    };

My array looks like this:

 Class1[] arr = new class1[7];

        arr[0] = new class1(101, "Some string", Class1.EnumList.Spring, 100, DateTime.Parse("10/13/2008"));
 .
 .
 array continues..

How do I sort by enum value?

2 Answers 2

3

You can use IEnumerable.OrderBy.

If you sort by an enum-value it will look at the underlying type (usually int) and sort by that. This means the best solution would just be like this:
arr.OrderBy(c => c.TheEnumProperty);

This method will return a sorted IEnumerable which you can cast back to an array with IEnumerable.ToArray.

Sign up to request clarification or add additional context in comments.

Comments

0

Something like

var sortedByEnum = arr.GroupBy(x => x.enum).OrderBy(group => group);

2 Comments

You can indent the code by 4 spaces to have it formatted as code.
yes sorry, maybe you can make the edit request again

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.