CHAPTER 12 LINQ TO OBJECTS
Sorting Expressions
As you have seen over this chapter’s initial examples, a query expression can take an orderby operator to
sort items in the subset by a specific value. By default, the order will be ascending; thus, ordering by a
string would be alphabetical, ordering by numerical data would be lowest to highest, and so forth. If you
need to view the results in a descending order, simply include the descending operator. Ponder the
following method:
static void AlphabetizeProductNames(ProductInfo[] products)
{
// Get names of products, alphabetized.
var subset = from p in products orderby p.Name select p;
Console.WriteLine("Ordered by Name:");
foreach (var p in subset)
{
Console.WriteLine(p.ToString());
}
}
Although ascending order is the defau