In LINQ, the OrderByDescending operator is used to sort the list/collection of items in descending order.
Syntax of LINQ OrderByDescending Operator
Following is the syntax of using the LINQ orderbydescending operator to sort the list of item values in descending order.
LINQ OrderByDescending Syntax in C#
var studentname = Objstudent.OrderByDescending(x => x.Name);
LINQ OrderByDescending Syntax in VB.NET
Dim studentname = Objstudent.OrderByDescending(Function(x) x.Name)
If you observe above syntax we are sorting Objstudent collection items using linq OrderByDescending operator.
LINQ OrderByDescending Operator Example
Following is the example of using the LINQ orderbydescending sorting operator to sort the list of items in descending order.
LINQ OrderByDescending Example in C#
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQExamples
{
class Program
{
static void Main(string[] args)
{
List<Student> Objstudent = new List<Student>()
{
new Student() { Name = "Suresh Dasari", Gender = "Male", Subjects = new List<string> { "Mathematics", "Physics" } },
new Student() { Name = "Rohini Alavala", Gender = "Female", Subjects = new List<string> { "Entomology", "Botany" } },
new Student() { Name = "Praveen Kumar", Gender = "Male", Subjects = new List<string> { "Computers", "Operating System", "Java" } },
new Student() { Name = "Sateesh Chandra", Gender = "Male", Subjects = new List<string> { "English", "Social Studies", "Chemistry" } },
new Student() { Name = "Madhav Sai", Gender = "Male", Subjects = new List<string> { "Accounting", "Charted" } }
};
var studentname = Objstudent.OrderByDescending(x => x.Name);
foreach (var student in studentname)
{
Console.WriteLine(student.Name);
}
Console.ReadLine();
}
}
class Student
{
public string Name { get; set; }
public string Gender { get; set; }
public List<string> Subjects { get; set; }
}
}
LINQ OrderByDescending Example in VB.NET
Module Module1
Sub Main()
Dim Objstudent As New List(Of Student)() From {
New Student() With {.Name = "Suresh Dasari", .Gender = "Male", .Subjects = New List(Of String)() From {"Mathematics", "Physics"}},
New Student() With {.Name = "Rohini Alavala", .Gender = "Female", .Subjects = New List(Of String)() From {"Entomology", "Botany"}},
New Student() With {.Name = "Praveen Kumar", .Gender = "Male", .Subjects = New List(Of String)() From {"Computers", "Operating System", "Java"}},
New Student() With {.Name = "Sateesh Chandra", .Gender = "Male", .Subjects = New List(Of String)() From {"English", "Social Studies", "Chemistry"}},
New Student() With {.Name = "Madhav Sai", .Gender = "Male", .Subjects = New List(Of String)() From {"Accounting", "Charted"}}
}
Dim studentname = Objstudent.OrderByDescending(Function(x) x.Name)
For Each student In studentname
Console.WriteLine(student.Name)
Next
Console.ReadLine()
End Sub
Class Student
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Gender() As String
Get
Return m_Gender
End Get
Set(ByVal value As String)
m_Gender = value
End Set
End Property
Private m_Gender As String
Public Property Subjects() As List(Of String)
Get
Return m_Subjects
End Get
Set(ByVal value As List(Of String))
m_Subjects = value
End Set
End Property
Private m_Subjects As List(Of String)
End Class
End Module
The above example shows that we used the OrderByDescending operator to sort list items in descending order.
Result of LINQ OrderByDescending Operator
Following is the result of using the LINQ OrderByDescending operator to sort list items in descending order.
Suresh Dasari
Sateesh Chandra
Rohini Alavala
Praveen Kumar
Madhav Sai
This is how we can use LINQ orderbydescending operator to sort the list of items in descending order.