C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Info: The identifier "element" is entirely arbitrary, and it is just declared in each individual query.
Ascending: We see that the first query expression and the second query expression both perform an ascending sort.
Tip: The word ascending means "lowest to highest", like how a staircase ascends from the lowest step to the highest step.
And: The final query returns a descending sort, with the highest numbers going to the lowest numbers. This is the logical opposite.
DescendingC# program that uses orderby clause
using System;
using System.Linq;
class Program
{
static void Main()
{
// Input array.
int[] array = { 2, 5, 3 };
// Use orderby, orderby ascending, and orderby descending.
var result0 = from element in array
orderby element
select element;
var result1 = from element in array
orderby element ascending
select element;
var result2 = from element in array
orderby element descending
select element;
// Print results.
Console.WriteLine("result0");
foreach (var element in result0)
{
Console.WriteLine(element);
}
Console.WriteLine("result1");
foreach (var element in result1)
{
Console.WriteLine(element);
}
Console.WriteLine("result2");
foreach (var element in result2)
{
Console.WriteLine(element);
}
}
}
Output
result0
2
3
5
result1
2
3
5
result2
5
3
2
System.Linq: The extension method calls are why the System.Linq namespace is required.
Info: The extensions OrderBy, and OrderByDescending are used when the C# compiler parses query expressions.
OrderBy, OrderByDescending