- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Orderby Descending
Use Orderby descending in C# to sort elements in descending order.
The following is our list −
IList<Employee> emp = new List<Employee>() { new Employee() { EmployeeRank = 3, EmpName = "Tom", EmpMarks = 90 } , new Employee() { EmployeeRank = 4, EmpName = "Katie", EmpMarks = 95 } };
Now to sort the list according to Student name, use Orderby. The default is ascending, therefore for descending order, use descending.
var res = from str in emp orderby str.EmpName descending select str;
Here is the complete code.
Example
using System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { IList<Employee> emp = new List<Employee>() { new Employee() { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 } , new Employee() { EmployeeRank = 05, EmpName = "Raman", EmpMarks = 95 } }; var res = from str in emp orderby str.EmpName descending select str; Console.WriteLine("Student List (Descending Order):"); foreach (var list in res) Console.WriteLine(list.EmpName); } } public class Employee { public int EmployeeRank { get; set; } public string EmpName { get; set; } public int EmpMarks { get; set; } }
Output
Student List (Descending Order): Raman Amit
Advertisements