A sorting operator arranges the elements of the collection in ascending or descending order.
Here is the example of sorting by name.The example shows that how sorting can be done ascending or descending order with LINQ query.
------------------------------------------------------------------------------------
Below is the example of sorting with Lambda expression.The query is sorted by name with Lambda expression both ascending or descending order.
Example
foreach (var obj in query2)
Console.ReadKey();
Output :
Here is the example of sorting by name.The example shows that how sorting can be done ascending or descending order with LINQ query.
Example
-------------------------------------------------------
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
var
students = new
student[]
{
new
student{name
= "John1",roll=123,age=12},
new
student{name
= "John2",roll=124,age=13},
new
student{name
= "John3",roll=125,age=14},
new
student{name
= "John4",roll=126,age=15},
new
student{name
= "John5",roll=127,age=16},
new
student{name
= "John6",roll=128,age=17},
new
student{name
= "John7",roll=129,age=18}
};
/************Example
of Order ***************************/
Console.WriteLine("*************Example
of Order by Name***********/");
var
query1 = from
s in
students
orderby
s.name
select
s;
foreach
(var
obj in
query1)
{
Console.WriteLine("Name
:{0} ,Age :{1},Roll :{2}",
obj.name, obj.age, obj.roll);
}
Console.WriteLine("*************Example
of Order by Name Decending***********/");
var
query2 = from
s in
students
orderby
s.name descending
select
s;
foreach
(var
obj in
query2)
{
Console.WriteLine("Name
:{0} ,Age :{1},Roll :{2}",
obj.name, obj.age, obj.roll);
}
Console.ReadKey();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
Output :
Output :
*************Example of Order by Name***********/
Name :John1 ,Age :12,Roll :123
Name :John2 ,Age :13,Roll :124
Name :John3 ,Age :14,Roll :125
Name :John4 ,Age :15,Roll :126
Name :John5 ,Age :16,Roll :127
Name :John6 ,Age :17,Roll :128
Name :John7 ,Age :18,Roll :129
*************Example of Order by Name Decending***********/
Name :John7 ,Age :18,Roll :129
Name :John6 ,Age :17,Roll :128
Name :John5 ,Age :16,Roll :127
Name :John4 ,Age :15,Roll :126
Name :John3 ,Age :14,Roll :125
Name :John2 ,Age :13,Roll :124
Name :John1 ,Age :12,Roll :123
Name :John1 ,Age :12,Roll :123
Name :John2 ,Age :13,Roll :124
Name :John3 ,Age :14,Roll :125
Name :John4 ,Age :15,Roll :126
Name :John5 ,Age :16,Roll :127
Name :John6 ,Age :17,Roll :128
Name :John7 ,Age :18,Roll :129
*************Example of Order by Name Decending***********/
Name :John7 ,Age :18,Roll :129
Name :John6 ,Age :17,Roll :128
Name :John5 ,Age :16,Roll :127
Name :John4 ,Age :15,Roll :126
Name :John3 ,Age :14,Roll :125
Name :John2 ,Age :13,Roll :124
Name :John1 ,Age :12,Roll :123
------------------------------------------------------------------------------------
Below is the example of sorting with Lambda expression.The query is sorted by name with Lambda expression both ascending or descending order.
Example
----------------------------------------------------------
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
var
students = new
student[]
{
new
student{name
= "John1",roll=123,age=12},
new
student{name
= "John2",roll=124,age=13},
new
student{name
= "John3",roll=125,age=14},
new
student{name
= "John4",roll=126,age=15},
new
student{name
= "John5",roll=127,age=16},
new
student{name
= "John6",roll=128,age=17},
new
student{name
= "John7",roll=129,age=18}
};
Console.WriteLine("*************Example
of Order by Name***********/");
var
query = students.OrderBy(a => a.name);
foreach
(var
obj in
query)
{
Console.WriteLine("Name
:{0} ,Age :{1},Roll :{2}",
obj.name, obj.age, obj.roll);
}
Console.WriteLine("*************Example
of Order by Name Decending***********/");
var
query2 = students.OrderByDescending(a => a.name);
foreach (var obj in query2)
{
Console.WriteLine("Name
:{0} ,Age :{1},Roll :{2}",
obj.name, obj.age, obj.roll);
}
Console.ReadKey();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
Output :
-------------------------------------------------------------------------------
*************Example of Order by Name***********/
Name :John1 ,Age :12,Roll :123
Name :John2 ,Age :13,Roll :124
Name :John3 ,Age :14,Roll :125
Name :John4 ,Age :15,Roll :126
Name :John5 ,Age :16,Roll :127
Name :John6 ,Age :17,Roll :128
Name :John7 ,Age :18,Roll :129
*************Example of Order by Name Decending***********/
Name :John7 ,Age :18,Roll :129
Name :John6 ,Age :17,Roll :128
Name :John5 ,Age :16,Roll :127
Name :John4 ,Age :15,Roll :126
Name :John3 ,Age :14,Roll :125
Name :John2 ,Age :13,Roll :124
Name :John1 ,Age :12,Roll :123
Name :John1 ,Age :12,Roll :123
Name :John2 ,Age :13,Roll :124
Name :John3 ,Age :14,Roll :125
Name :John4 ,Age :15,Roll :126
Name :John5 ,Age :16,Roll :127
Name :John6 ,Age :17,Roll :128
Name :John7 ,Age :18,Roll :129
*************Example of Order by Name Decending***********/
Name :John7 ,Age :18,Roll :129
Name :John6 ,Age :17,Roll :128
Name :John5 ,Age :16,Roll :127
Name :John4 ,Age :15,Roll :126
Name :John3 ,Age :14,Roll :125
Name :John2 ,Age :13,Roll :124
Name :John1 ,Age :12,Roll :123