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
Orderby clause in C#
The orderby clause in C# is used to sort elements in a collection based on one or more specified criteria. It supports both ascending (default) and descending order, and can be used with LINQ query expressions or method syntax.
Syntax
Following is the syntax for using orderby in query expression −
var result = from element in collection
orderby element.Property [ascending|descending]
select element;
Following is the syntax for method syntax using OrderBy() and OrderByDescending() −
var result = collection.OrderBy(x => x.Property); var result = collection.OrderByDescending(x => x.Property);
Using orderby for Ascending Order
By default, orderby sorts elements in ascending order. The ascending keyword is optional −
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
List<int> numbers = new List<int> { 45, 12, 78, 23, 56 };
var ascendingOrder = from num in numbers
orderby num ascending
select num;
Console.WriteLine("Ascending order:");
foreach (int num in ascendingOrder) {
Console.WriteLine(num);
}
}
}
The output of the above code is −
Ascending order: 12 23 45 56 78
Using orderby for Descending Order
Use the descending keyword to sort elements in reverse order −
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
List<string> myList = new List<string>();
myList.Add("iOS by Apple");
myList.Add("Android by Google");
myList.Add("Symbian by Nokia");
var myLen = from element in myList
orderby element.Length descending
select element;
Console.WriteLine("Descending order by length:");
foreach (string str in myLen) {
Console.WriteLine(str);
}
}
}
The output of the above code is −
Descending order by length: Android by Google Symbian by Nokia iOS by Apple
Multiple Sorting Criteria
You can sort by multiple fields using comma-separated criteria −
using System;
using System.Collections.Generic;
using System.Linq;
class Student {
public string Name { get; set; }
public int Age { get; set; }
public double Grade { get; set; }
}
class Demo {
static void Main() {
List<Student> students = new List<Student> {
new Student { Name = "Alice", Age = 22, Grade = 85.5 },
new Student { Name = "Bob", Age = 20, Grade = 92.0 },
new Student { Name = "Charlie", Age = 22, Grade = 78.5 }
};
var sortedStudents = from student in students
orderby student.Age ascending, student.Grade descending
select student;
Console.WriteLine("Sorted by Age (asc), then Grade (desc):");
foreach (var student in sortedStudents) {
Console.WriteLine($"{student.Name}: Age {student.Age}, Grade {student.Grade}");
}
}
}
The output of the above code is −
Sorted by Age (asc), then Grade (desc): Bob: Age 20, Grade 92 Alice: Age 22, Grade 85.5 Charlie: Age 22, Grade 78.5
Comparison: Query vs Method Syntax
| Query Expression | Method Syntax |
|---|---|
orderby element ascending |
.OrderBy(x => x) |
orderby element descending |
.OrderByDescending(x => x) |
orderby x.A, x.B descending |
.OrderBy(x => x.A).ThenByDescending(x => x.B) |
Conclusion
The orderby clause in C# provides a simple way to sort collections based on one or multiple criteria. It supports both ascending and descending orders, and can be combined with other LINQ operations to create powerful data queries for organizing and retrieving sorted data efficiently.
