A sorting operation orders the elements of a sequence based on one or more attributes.

Methods

MethodDescriptionQuery expression
OrderBySort values in ascending orderorderby
ThenByPerform a secondary sort in ascending orderorderby …, …
ReverseReverse the order of the elementsN/A

Query Expression

Sort Ascending

string[] words = { "the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" };

var query = from word in words
    orderby word.Length
    select word;

Sort Secondary Ascending

var query = from word in words
    order by word.Length, // First, sort by length
    word.Substring(0, 1) // then by the first letter of the word
    select word;