Linq Array Order-By
Here is very simple example of ordering an array of string using Linq expression. We will sort array of string.
C# Linq sort array ascending.
string[] arrOfStrings = new string[]
{
"Linq",
"WCF",
"WPF",
"SilverLight",
".Net",
"IIS7",
"HTML5",
"Android"
};
// Linq expression to sort an array of strings.
var sortResults = from s in arrOfStrings
orderby s
select s;
foreach (string str in sortResults)
{
Console.WriteLine(str);
}
Output
.Net
Android
HTML5
IIS7
Linq
SilverLight
WCF
WPF
Above example uses LINQ expression as:
var sortResults = from s in arrOfStrings
orderby s
select s;
The default order by clause does sorting in ascending order.
Linq Sort order by descending.
Let’s sort an array of string in descending order. It will reverse the array output in result.
string[] arrOfStrings = new string[]
{
"Linq",
"WCF",
"WPF",
"SilverLight",
".Net",
"IIS7",
"HTML5",
"Android"
};
var sortResults = from str in arrOfStrings
orderby str descending
select str;
foreach (string str in sortResults)
{
Console.WriteLine(str);
}
Output :
WPF
WCF
SilverLight
Linq
IIS7
HTML5
Android
.Net
Currently rated 3.3 by 6 people
- Currently 3.333333/5 Stars.
- 1
- 2
- 3
- 4
- 5