

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort an array in descending order using C#
Declare an array and initialize −
int[] arr = new int[] { 87, 23, 65, 29, 67 };
To sort, use the Sort() method and CompareTo() to compare and display in decreasing order −
Array.Sort < int > (arr, new Comparison < int > ((val1, val2) => val2.CompareTo(val1)));
Let us see the complete code −
Example
using System; using System.Collections.Generic; using System.Text; public class Demo { public static void Main(string[] args) { int[] arr = new int[] { 87, 23, 65, 29, 67 }; // Initial Array Console.WriteLine("Initial Array..."); foreach(int items in arr) { Console.WriteLine(items); } Array.Sort < int > (arr, new Comparison < int > ((val1, val2) => val2.CompareTo(val1))); // Sorted Array Console.WriteLine("Sorted Array in decreasing order..."); foreach(int items in arr) { Console.WriteLine(items); } } }
Output
Initial Array... 87 23 65 29 67 Sorted Array in decreasing order... 87 67 65 29 23
- Related Questions & Answers
- C# program to sort an array in descending order
- C program to sort an array in descending order
- 8086 program to sort an integer array in descending order
- How do you sort an array in C# in descending order?
- How to sort one dimensional array in descending order using Array Class method?
- Python program to sort the elements of an array in descending order
- Sort MongoDB documents in descending order
- How to sort an ArrayList in Java in descending order?
- How to sort an ArrayList in Descending Order in Java
- How to sort one dimensional array in descending order using non static method?
- Sort ArrayList in Descending order using Comparator with Java Collections
- Sort list elements in descending order in C#
- How to sort List in descending order using Comparator in Java
- How to perform descending order sort in MongoDB?
- How to sort a Vector in descending order using STL in C++?
Advertisements