
- 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
C# program to sort an array in descending order
Initialize the array.
int[] myArr = new int[5] {98, 76, 99, 32, 77};
Compare the first element in the array with the next element to find the largest element, then the second largest, etc.
if(myArr[i] < myArr[j]) { temp = myArr[i]; myArr[i] = myArr[j]; myArr[j] = temp; }
Above, i and j are initially set to.
i=0; j=i+1;
Try to run the following code to sort an array in descending order.
Example
using System; public class Demo { public static void Main() { int[] myArr = new int[5] {98, 76, 99, 32, 77}; int i, j, temp; Console.Write("Elements:
"); for(i=0;i<5;i++) { Console.Write("{0} ",myArr[i]); } for(i=0; i<5; i++) { for(j=i+1; j<5; j++) { if(myArr[i] < myArr[j]) { temp = myArr[i]; myArr[i] = myArr[j]; myArr[j] = temp; } } } Console.Write("
Descending order:
"); for(i=0; i<5; i++) { Console.Write("{0} ", myArr[i]); } Console.Write("
"); } }
Output
Elements: 98 76 99 32 77 Descending order: 99 98 77 76 32
- Related Articles
- C program to sort an array in descending order
- 8086 program to sort an integer array in descending order
- Golang Program to sort an array in descending order using insertion sort
- 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
- Sort an array in descending order using C#
- How do you sort an array in C# in descending order?
- 8085 Program to perform selection sort in descending order
- 8085 Program to perform bubble sort in descending order
- C# Program to order array elements in descending order
- How to sort an ArrayList in Descending Order in Java
- How to sort an ArrayList in Java in descending order?
- C program to sort an array in an ascending order

Advertisements