c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
Linq

order by in linq c#

| | ASP-NET , Linq

This sample uses orderby to sort a list of words alphabetically.


public void orderbyasc() 
{
string[] words = { "cherry", "apple", "banana" };

var sortedWords =
from w in words
orderby w
select w;
Console.WriteLine("The sorted list of words:");
foreach (var w in sortedWords)
{
Console.WriteLine(w);
}
}


output:

The sorted list of words:

apple
banana
cherry

order by descending in linq


public void orderbydesc() 
{
string[] words = { "cherry", "apple", "banana" };
var sortedWords =
from w in words
orderby w descending
select w;

Console.WriteLine("The sorted list of words:");
foreach (var w in sortedWords)
{
Console.WriteLine(w);
}
}

output:
The sorted list of words:

cherry
banana
apple