Permalink
Fetching contributors…
Cannot retrieve contributors at this time
57 lines (50 sloc) 2.48 KB
title ms.date ms.prod ms.technology ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author translation.priority.ht
orderby clause (C# Reference)
2015-07-20
.net
devlang-csharp
article
orderby
orderby_CSharpKeyword
CSharp
orderby clause [C#]
orderby keyword [C#]
21f87f48-d69d-4e95-9a52-6fec47b37e1f
17
BillWagner
wiwagn
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

orderby clause (C# Reference)

In a query expression, the orderby clause causes the returned sequence or subsequence (group) to be sorted in either ascending or descending order. Multiple keys can be specified in order to perform one or more secondary sort operations. The sorting is performed by the default comparer for the type of the element. The default sort order is ascending. You can also specify a custom comparer. However, it is only available by using method-based syntax. For more information, see Sorting Data.

Example

In the following example, the first query sorts the words in alphabetical order starting from A, and second query sorts the same words in descending order. (The ascending keyword is the default sort value and can be omitted.)

[!code-cscscsrefQueryKeywords#20]

Example

The following example performs a primary sort on the students' last names, and then a secondary sort on their first names.

[!code-cscscsrefQueryKeywords#22]

Remarks

At compile time, the orderby clause is translated to a call to the xref:System.Linq.Enumerable.OrderBy%2A method. Multiple keys in the orderby clause translate to xref:System.Linq.Enumerable.ThenBy%2A method calls.

See Also

C# Reference
Query Keywords (LINQ)
LINQ Query Expressions
group clause
Getting Started with LINQ in C#