O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Ordering Using the orderby Keyword

Sorting elements by one or more criteria is another commonly used querying action, which is supported through the orderby keyword. The basic idea is to specify a key selector function that selects an ordering key for each element in the input sequence. Upon enumerating over the query result object, the ordering key is obtained for all elements and used to sort the elements. Ordering by a key can be done either ascending (the default) or descending, using the corresponding keywords.

Besides doing just a single ordering (hence selecting just a single key), it’s possible to specify secondary and n-ary orderings, too. For example, you might want to sort a list of products first by price in a descending fashion, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required