6.4. Sort Data Using LINQ

Problem

You need to ensure that the results of a query are sorted appropriately, or you just need to sort the elements in a collection or array.

Solution

Create a standard LINQ query, such as the one described in recipe 6-1, and use the Order By clause to ensure that the data is ordered correctly.

How It Works

If you are familiar with query languages, you should recognize the Order By clause. It is used to specify how the data returned from a query is sorted. The Order By clause also supports the optional Ascending and Descending keywords, which specify in which direction the data is sorted. If omitted, Ascending is used by default. An Order By clause might look something like this:

Order By book.Title Ascending

The ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.