Sorting in Arrays
By Santhosh N
This explains how to sort the array elements in ascending and descending order in .Net.
There are couple of methods available in Array class to sort the elements inside
the Array in C#.
Sort method sorts the elements in ascending order and
to get the descending order you can use Reverse method to sort them in descending
order.
Sample Code:
int[] arr = new int[4];
arr[0]
= 10;
arr[1]
= 20;
arr[2]
= 15;
arr[3]
= 25;
//sort the elements in ascending order and display
Array.Sort(arr);
for (int cnt = 0; cnt < arr.Length; cnt++)
label1.Text += "\nCnt " + cnt + ": " + arr[cnt];
//sort the elements in descending order and display
Array.Reverse(arr);
for (int cnt = 0; cnt < arr.Length; cnt++)
label1.Text += "\nCnt " + cnt + ": " + arr[cnt];
Sorting in Arrays (989 Views)