Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - SORT ARRAY ELEMENTS


How to sort a String type Array in Ascending Order?


The Sort() method is used to sort a String Array in Ascending order.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        string[] arr = {"Mohan", "John", "Paul", "Kriti", "Salim"};
        System.Array.Sort(arr);
    
        System.Console.WriteLine("The Sorted Array in ascending order is : ");
    
        foreach (string str in arr) {
            System.Console.WriteLine(str);
        }
    }    
}


Output :



  The Sorted Array in ascending order is :
  John
  Kriti
  Mohan
  Paul
  Salim

So, in the above code we have created a Array and initialised to the variable arr.


string[] arr = {"Mohan", "John", "Paul", "Kriti", "Salim"};

Below is how the values are positioned in the Array,

C_Sharp

Then we have used the System.Array.Sort() method to sort the Array arr in ascending order.


System.Array.Sort(arr);

And the Array arr gets sorted with John as the first value, Kriti second and Mohan as the third, Paul as fourth and Salim as the fifth value.

C_Sharp

And we get the below output.

Output :



  The Sorted Array in ascending order is :
  John
  Kriti
  Mohan
  Paul
  Salim

How to sort a Array with numbers in Increasing Order?


Even here the sort() method is used to sort the numbers in Increasing Order.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        int[] arr = {5, 3, 2, 4};
        System.Array.Sort(arr);
    
        System.Console.WriteLine("The Sorted Array in ascending order is : ");
    
        foreach (int str in arr) {
            System.Console.WriteLine(str);
        }
    }    
}


Output :



  The Sorted Array in ascending order is :
  2
  3
  4
  5

So, in the above code we have created an Integer Array and initialised to the variable arr.


int[] arr = {5, 3, 2, 4};

Below is how the values are positioned in the Array,

C_Sharp

Then we have used the Ints() Method from the sort package to sort the Array arr in increasing order.


System.Array.Sort(arr);

And the numbers in the Array arr gets sorted.

C_Sharp

And we get the below output.

Output :



  The Sorted Array in ascending order is :
  2
  3
  4
  5

How to sort a Array with numbers in Decreasing Order?


To sort an array in descending order, we will perform a trick.


Let us see in the below example.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        int[] arr = {5, 3, 2, 4};
        System.Array.Sort(arr);
        
        System.Array.Reverse(arr);
    
        System.Console.WriteLine("The Sorted Array in descending order is : ");
    
        foreach (int str in arr) {
            System.Console.WriteLine(str);
        }
    }    
}


Output :



  The Sorted Array in decreasing order is :
  5
  4
  3
  2

So, in the above code, all we have done is, used the Sort() method to sort the array in ascending order.


System.Array.Sort(arr);

Then we have used, the Reverse() method to reverse the elements of the array.


System.Array.Reverse(arr);

And we get the array elements sorted in descending order.