In this example, we’ll learn How to Sort an array in descending order without using inbuilt C# function.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
static void Main(string[] args) { int[] intArray = new int[] {2,9,4,3,5,1,7 }; int temp = 0; for (int i = 0; i <= intArray.Length-1; i++) { for (int j = i+1; j < intArray.Length; j++) { if (intArray[i] < intArray[j]) { temp = intArray[i]; intArray[i] = intArray[j]; intArray[j] = temp; } } } Console.WriteLine("Array sort in descending order"); foreach (var item in intArray) { Console.WriteLine(item); } Console.ReadLine(); } |
Output: