4

I have the following LINQ OrderBy clause:

Menu[] sorted = ret.OrderBy(menu => menu.Title).ToArray();

This works great, as menus are sorted in the order of their title.

However, if the title happens to be "Favorites", I want it to always be the first element in the array no matter what.

What's the easiest way to do this? Thanks!

0

1 Answer 1

15

I'd say:

Menu[] sorted = ret.OrderBy(menu => menu.Title != "Favorites")
                   .ThenBy(menu => menu.Title)
                   .ToArray();
3
  • I think you should use OrderByDescending in the first statement
    – EgorBo
    Commented Dec 20, 2011 at 6:45
  • Yea, or just do a != instead of == - Either way, this works great! Commented Dec 20, 2011 at 6:46
  • 1
    No, it's interpreting true as 1: an OrderBy goes from lowest to highest, so from 0 to 1, so from false to true.
    – user743382
    Commented Dec 20, 2011 at 6:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.