
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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 Articles
- Golang Program to sort an array in descending order using insertion sort
- 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
- Golang Program To Sort The Elements Of An Array In Descending Order
- Swift Program to Sort the Elements of an Array in Descending Order
- C++ Program to Sort the Elements of an Array in Descending Order
- How to sort one dimensional array in descending order using non static method?
- Sort MongoDB documents in descending order
- How to sort an ArrayList in Descending Order in Java
- How to sort an ArrayList in Java in descending order?
- Sort ArrayList in Descending order using Comparator with Java Collections

Advertisements