Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Orderby Descending
The orderby descending clause in C# is used with LINQ to sort collections in descending order. It works with both query syntax and method syntax, allowing you to sort objects by any property or field.
Syntax
Following is the syntax for using orderby descending with LINQ query syntax −
var result = from item in collection
orderby item.Property descending
select item;
Following is the syntax for using OrderByDescending() method syntax −
var result = collection.OrderByDescending(item => item.Property);
Using OrderBy Descending with Query Syntax
The following example demonstrates sorting a list of employees by name in descending order using LINQ query syntax −
using System;
using System.Linq;
using System.Collections.Generic;
public class Employee {
public int EmployeeRank { get; set; }
public string EmpName { get; set; }
public int EmpMarks { get; set; }
}
public class Demo {
public static void Main() {
IList<Employee> emp = new List<Employee>() {
new Employee() { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 },
new Employee() { EmployeeRank = 5, EmpName = "Raman", EmpMarks = 95 },
new Employee() { EmployeeRank = 3, EmpName = "Tom", EmpMarks = 85 },
new Employee() { EmployeeRank = 2, EmpName = "Katie", EmpMarks = 92 }
};
var res = from str in emp orderby str.EmpName descending select str;
Console.WriteLine("Employee List (Descending Order by Name):");
foreach (var employee in res)
Console.WriteLine(employee.EmpName + " - Marks: " + employee.EmpMarks);
}
}
The output of the above code is −
Employee List (Descending Order by Name): Tom - Marks: 85 Raman - Marks: 95 Katie - Marks: 92 Amit - Marks: 90
Using OrderByDescending Method Syntax
You can achieve the same result using method syntax with the OrderByDescending() method −
using System;
using System.Linq;
using System.Collections.Generic;
public class Employee {
public int EmployeeRank { get; set; }
public string EmpName { get; set; }
public int EmpMarks { get; set; }
}
public class Demo {
public static void Main() {
IList<Employee> emp = new List<Employee>() {
new Employee() { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 },
new Employee() { EmployeeRank = 5, EmpName = "Raman", EmpMarks = 95 },
new Employee() { EmployeeRank = 3, EmpName = "Tom", EmpMarks = 85 }
};
var resByName = emp.OrderByDescending(e => e.EmpName);
var resByMarks = emp.OrderByDescending(e => e.EmpMarks);
Console.WriteLine("Sorted by Name (Descending):");
foreach (var employee in resByName)
Console.WriteLine(employee.EmpName);
Console.WriteLine("\nSorted by Marks (Descending):");
foreach (var employee in resByMarks)
Console.WriteLine(employee.EmpName + ": " + employee.EmpMarks);
}
}
The output of the above code is −
Sorted by Name (Descending): Tom Raman Amit Sorted by Marks (Descending): Raman: 95 Amit: 90 Tom: 85
Multiple Level Sorting
You can sort by multiple criteria using ThenByDescending() for additional sorting levels −
using System;
using System.Linq;
using System.Collections.Generic;
public class Employee {
public int EmployeeRank { get; set; }
public string EmpName { get; set; }
public int EmpMarks { get; set; }
}
public class Demo {
public static void Main() {
IList<Employee> emp = new List<Employee>() {
new Employee() { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 },
new Employee() { EmployeeRank = 4, EmpName = "Raman", EmpMarks = 95 },
new Employee() { EmployeeRank = 3, EmpName = "Tom", EmpMarks = 85 },
new Employee() { EmployeeRank = 3, EmpName = "Katie", EmpMarks = 92 }
};
var result = emp.OrderByDescending(e => e.EmployeeRank)
.ThenByDescending(e => e.EmpMarks);
Console.WriteLine("Sorted by Rank (Desc), then by Marks (Desc):");
foreach (var employee in result)
Console.WriteLine($"Rank: {employee.EmployeeRank}, Name: {employee.EmpName}, Marks: {employee.EmpMarks}");
}
}
The output of the above code is −
Sorted by Rank (Desc), then by Marks (Desc): Rank: 4, Name: Raman, Marks: 95 Rank: 4, Name: Amit, Marks: 90 Rank: 3, Name: Katie, Marks: 92 Rank: 3, Name: Tom, Marks: 85
Comparison
| Query Syntax | Method Syntax |
|---|---|
orderby property descending |
OrderByDescending(x => x.property) |
| More readable for complex queries | More concise for simple operations |
| SQL-like syntax | Fluent interface with method chaining |
Conclusion
The orderby descending clause and OrderByDescending() method provide flexible ways to sort collections in descending order. Use query syntax for readability in complex scenarios, or method syntax for simple sorting operations and method chaining.
