C# Program to sort an array in descending order


In this program, You will learn how to sort an array in descending order in C#.


List is: 1 4 2 3 5

After sort list is: 5 4 3 2 1

Example: How to sort an array in descending order in C#.

using System;
public class Program
{
	public static void Main(string[] args)
	{
		int temp;
		int[] arr = new int[5];

		Console.Write("Enter five numbers:");
		for (int i = 0; i < arr.Length; i++)
		{
			arr[i] = Convert.ToInt32(Console.ReadLine());
		}

		for (int i = 0; i < arr.Length; i++)
		{
			for (int j = 1 + i; j < arr.Length; j++)
			{
				if (arr[i] < arr[j])
				{
					temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
		}

		Console.Write("Array list after sorting:");
		for (int i = 0; i < arr.Length; i++)
		{
			Console.Write(arr[i] + " ");
		}
	}
}

Output:

Enter five numbers:30
40
50
20
10
Array list after sorting:50 40 30 20 10 

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.