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# Program To Sort Student Names in Descending Order Using LINQ
This tutorial demonstrates how to create a C# program that sorts student names in descending order using Language Integrated Query (LINQ). LINQ provides a powerful, readable way to query and manipulate data collections directly within C# code.
Why Use LINQ for Sorting?
LINQ offers several advantages over traditional sorting approaches
-
Readability Clean, SQL-like syntax that's easy to understand
-
Flexibility Works with various data sources (collections, XML, databases) using the same syntax
-
Abstraction Focus on query logic rather than implementation details
-
Lazy Evaluation Query execution is deferred until results are needed, improving performance
Syntax
Following is the syntax for sorting using LINQ query expression
var sortedData = from item in collection
orderby item descending
select item;
Following is the syntax using LINQ method syntax
var sortedData = collection.OrderByDescending(item => item);
Using LINQ Query Expression
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Program {
public static void Main() {
// List of student names
List<string> students = new List<string>() {
"John",
"Steve",
"Bill",
"Maria",
"Julia",
"Paul"
};
// Use LINQ query expression to sort students in descending order
var sortedStudents = from student in students
orderby student descending
select student;
Console.WriteLine("Student names in descending order:");
foreach (var student in sortedStudents) {
Console.WriteLine(student);
}
}
}
The output of the above code is
Student names in descending order: Steve Paul Maria Julia John Bill
Using LINQ Method Syntax
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<string> students = new List<string>() {
"Alice", "David", "Charlie", "Bob", "Emma"
};
// Using method syntax with OrderByDescending
var sortedStudents = students.OrderByDescending(name => name);
Console.WriteLine("Sorted using method syntax:");
foreach (var student in sortedStudents) {
Console.WriteLine(student);
}
}
}
The output of the above code is
Sorted using method syntax: Emma David Charlie Bob Alice
Sorting by Custom Criteria
You can also sort by custom criteria, such as the length of student names
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<string> students = new List<string>() {
"Jo", "Alexander", "Ben", "Catherine", "Sam"
};
// Sort by name length in descending order
var sortedByLength = from student in students
orderby student.Length descending
select student;
Console.WriteLine("Students sorted by name length (longest first):");
foreach (var student in sortedByLength) {
Console.WriteLine($"{student} (Length: {student.Length})");
}
}
}
The output of the above code is
Students sorted by name length (longest first): Alexander (Length: 9) Catherine (Length: 9) Ben (Length: 3) Sam (Length: 3) Jo (Length: 2)
Comparison: LINQ vs Traditional Sorting
| LINQ Approach | Traditional Array.Sort() |
|---|---|
| Declarative syntax - specify what you want | Imperative - specify how to do it |
| Lazy evaluation - executed when needed | Immediate execution |
| Immutable - returns new sequence | Mutable - modifies original array |
| Works with any IEnumerable | Limited to arrays and specific collections |
Conclusion
LINQ provides an elegant and powerful way to sort student names in descending order using either query expression or method syntax. The declarative nature of LINQ makes code more readable and maintainable compared to traditional sorting methods, while offering flexibility to sort by various criteria beyond simple alphabetical ordering.
