Sorting Data
A sorting operation orders the elements of a sequence based on one or more attributes. The first sort criterion performs a primary sort on the elements. By specifying a second sort criterion, you can sort the elements within each primary sort group.
The following illustration shows the results of an alphabetical sort operation on a sequence of characters.
The standard query operator methods that sort data are listed in the following section.
Methods
Method Name |
Description |
C# Query Expression Syntax |
Visual Basic Query Expression Syntax |
More Information |
---|---|---|---|---|
OrderBy |
Sorts values in ascending order. |
orderby |
Order By |
|
OrderByDescending |
Sorts values in descending order. |
orderby … descending |
Order By … Descending |
|
ThenBy |
Performs a secondary sort in ascending order. |
orderby …, … |
Order By …, … |
|
ThenByDescending |
Performs a secondary sort in descending order. |
orderby …, … descending |
Order By …, … Descending |
|
Reverse |
Reverses the order of the elements in a collection. |
Not applicable. |
Not applicable. |
Query Expression Syntax Examples
Primary Sort Examples
Primary Ascending Sort
The following example demonstrates how to use the orderby (Order By in Visual Basic) clause in a LINQ query to sort the strings in an array by string length, in ascending order.
Dim words = {"the", "quick", "brown", "fox", "jumps"}
Dim sortQuery = From word In words
Order By word.Length
Select word
Dim sb As New System.Text.StringBuilder()
For Each str As String In sortQuery
sb.AppendLine(str)
Next
' Display the results.
MsgBox(sb.ToString())
' This code produces the following output:
' the
' fox
' quick
' brown
' jumps
string[] words = { "the", "quick", "brown", "fox", "jumps" };
IEnumerable<string> query = from word in words
orderby word.Length
select word;
foreach (string str in query)
Console.WriteLine(str);
/* This code produces the following output:
the
fox
quick
brown
jumps
*/
Primary Descending Sort
The next example demonstrates how to use the orderbydescending (Order By Descending in Visual Basic) clause in a LINQ query to sort the strings by their first letter, in descending order.
Dim words = {"the", "quick", "brown", "fox", "jumps"}
Dim sortQuery = From word In words
Order By word.Substring(0, 1) Descending
Select word
Dim sb As New System.Text.StringBuilder()
For Each str As String In sortQuery
sb.AppendLine(str)
Next
' Display the results.
MsgBox(sb.ToString())
' This code produces the following output:
' the
' quick
' jumps
' fox
' brown
string[] words = { "the", "quick", "brown", "fox", "jumps" };
IEnumerable<string> query = from word in words
orderby word.Substring(0, 1) descending
select word;
foreach (string str in query)
Console.WriteLine(str);
/* This code produces the following output:
the
quick
jumps
fox
brown
*/
Secondary Sort Examples
Secondary Ascending Sort
The following example demonstrates how to use the orderby (Order By in Visual Basic) clause in a LINQ query to perform a primary and secondary sort of the strings in an array. The strings are sorted primarily by length and secondarily by the first letter of the string, both in ascending order.
Dim words = {"the", "quick", "brown", "fox", "jumps"}
Dim sortQuery = From word In words
Order By word.Length, word.Substring(0, 1)
Select word
Dim sb As New System.Text.StringBuilder()
For Each str As String In sortQuery
sb.AppendLine(str)
Next
' Display the results.
MsgBox(sb.ToString())
' This code produces the following output:
' fox
' the
' brown
' jumps
' quick
string[] words = { "the", "quick", "brown", "fox", "jumps" };
IEnumerable<string> query = from word in words
orderby word.Length, word.Substring(0, 1)
select word;
foreach (string str in query)
Console.WriteLine(str);
/* This code produces the following output:
fox
the
brown
jumps
quick
*/
Secondary Descending Sort
The next example demonstrates how to use the orderbydescending (Order By Descending in Visual Basic) clause in a LINQ query to perform a primary sort, in ascending order, and a secondary sort, in descending order. The strings are sorted primarily by length and secondarily by the first letter of the string.
Dim words = {"the", "quick", "brown", "fox", "jumps"}
Dim sortQuery = From word In words
Order By word.Length, word.Substring(0, 1) Descending
Select word
Dim sb As New System.Text.StringBuilder()
For Each str As String In sortQuery
sb.AppendLine(str)
Next
' Display the results.
MsgBox(sb.ToString())
' This code produces the following output:
' fox
' the
' quick
' jumps
' brown
string[] words = { "the", "quick", "brown", "fox", "jumps" };
IEnumerable<string> query = from word in words
orderby word.Length, word.Substring(0, 1) descending
select word;
foreach (string str in query)
Console.WriteLine(str);
/* This code produces the following output:
the
fox
quick
jumps
brown
*/
See Also
Tasks
How to: Order the Results of a Join Clause (C# Programming Guide)
How to: Sort Query Results by Using LINQ (Visual Basic)
How to: Sort or Filter Text Data by Any Word or Field (LINQ)
Reference
Order By Clause (Visual Basic)