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 order array elements in descending order
To order array elements in descending order in C#, you can use various approaches including Array.Sort() with reversal, LINQ's OrderByDescending(), or custom comparison methods. These methods provide flexibility for sorting different data types and applying custom sorting criteria.
Syntax
Following is the syntax for basic descending sort using Array.Sort() and Array.Reverse() −
Array.Sort(arrayName); Array.Reverse(arrayName);
Following is the syntax for LINQ OrderByDescending() −
var result = array.OrderByDescending(element => element);
Using Array.Sort() and Array.Reverse()
The simplest approach is to sort the array in ascending order first, then reverse it −
using System;
public class Demo {
public static void Main() {
int[] numbers = { 45, 12, 78, 23, 56, 89, 34 };
Console.WriteLine("Original array:");
Console.WriteLine(string.Join(", ", numbers));
Array.Sort(numbers);
Array.Reverse(numbers);
Console.WriteLine("Descending order:");
Console.WriteLine(string.Join(", ", numbers));
}
}
The output of the above code is −
Original array: 45, 12, 78, 23, 56, 89, 34 Descending order: 89, 78, 56, 45, 34, 23, 12
Using LINQ OrderByDescending()
LINQ provides a more flexible approach with OrderByDescending() method −
using System;
using System.Linq;
public class Demo {
public static void Main() {
string[] cities = { "Paris", "London", "Tokyo", "New York", "Berlin" };
Console.WriteLine("Original array:");
Console.WriteLine(string.Join(", ", cities));
var sortedCities = cities.OrderByDescending(city => city).ToArray();
Console.WriteLine("Alphabetically descending:");
Console.WriteLine(string.Join(", ", sortedCities));
}
}
The output of the above code is −
Original array: Paris, London, Tokyo, New York, Berlin Alphabetically descending: Tokyo, Paris, New York, London, Berlin
Using Custom Sorting Criteria
You can sort by specific properties like string length using lambda expressions −
using System;
using System.Linq;
public class Demo {
public static void Main() {
string[] devices = { "Keyboard", "Laptop", "Mouse", "Monitor" };
Console.WriteLine("Original array:");
Console.WriteLine(string.Join(", ", devices));
var sortedByLength = devices.OrderByDescending(device => device.Length)
.ThenBy(device => device)
.ToArray();
Console.WriteLine("Sorted by length (desc), then alphabetically:");
foreach (string device in sortedByLength) {
Console.WriteLine($"{device} (Length: {device.Length})");
}
}
}
The output of the above code is −
Original array: Keyboard, Laptop, Mouse, Monitor Sorted by length (desc), then alphabetically: Keyboard (Length: 8) Monitor (Length: 7) Laptop (Length: 6) Mouse (Length: 5)
Comparison of Sorting Methods
| Method | Best For | Modifies Original |
|---|---|---|
| Array.Sort() + Array.Reverse() | Simple numeric/string arrays | Yes |
| LINQ OrderByDescending() | Complex sorting criteria | No (returns new sequence) |
| Array.Sort() with custom comparer | Complex objects with custom logic | Yes |
Conclusion
C# offers multiple ways to sort arrays in descending order. Use Array.Sort() with Array.Reverse() for simple cases, or LINQ's OrderByDescending() for more complex sorting criteria. Choose the method that best fits your specific sorting requirements and whether you need to preserve the original array.
