Java Sort Array

Hi guys!! Welcome to flower brackets blog. In this post we are going to learn java sort array.

java sort array

We can use arrays.sort method to sort elements in an array. Either it might be ascending or descending order.

Java sort array

arrays.sort() method can be found in java.util.Arrays class.

arrays.sort() syntax:

public static void sort(int[] arr, int from_Index, int to_Index)

arr - array to be sorted
from_Index - index of the first element to be sorted, inclusive.
to_Index - index of the last element to be sorted, exclusive.

sort() method will not return any value.

We can do both sorting in ascending and descending using arrays.sort method. Here’s the example using arrays.sort method,

import java.util.Arrays;

public class JavaSort 
{
   public static void main(String[] args) 
   {		
      int arrNum[] = {14, 5, 8, 23, 100, 85};
 
      Arrays.sort(arrNum); // arrays.sort method by default sorts in ascending order
 
      System.out.printf("Sorted array java : %s", Arrays.toString(arrNum));
   }
}

Output :

Sorted array java : [5, 8, 14, 23, 85, 100]


Now let’s learn how to sort a subarray in java using arrays.sort. Here’s the example,

import java.util.Arrays;

public class SortJava 
{
   public static void main(String[] args) 
   { 
      int arrDemo[] = {14, 8, 5, 54, 41, 10, 1, 500};

      // here we are sorting subarray elements only i.e, {8, 5, 54, 41} 
      Arrays.sort(arrDemo, 1, 5);

      System.out.printf("Sorted array java : %s", Arrays.toString(arrDemo));
   }
}

Output :

Sorted array java : [14, 5, 8, 41, 54, 10, 1, 500]


Next, using the same example we are going to sort subarray java in descending order using arrays.sort

import java.util.Arrays;
import java.util.Collections;

public class JavaArraySort 
{
   public static void main(String[] args) 
   {
      Integer arrNum[] = {14, 8, 5, 54, 41, 10, 1, 500};

      // sorting array in java in descending order
      Arrays.sort(arrNum, Collections.reverseOrder());

      System.out.printf("Sorted array java : %s", Arrays.toString(arrNum));
   }
}

Output :

Sorted array java : [500, 54, 41, 14, 10, 8, 5, 1]


Example : java sort array

Now we will see how to print the elements in descending order. So in order to print the elements in the descending order, same method is used as above.

Also Read – Sort String Array Java

That is arrays.sort. But earlier we used only simple sort method which accepts string array.

Now it accepts comparator to print values in reverse or descending order. So we need to call the utility method in collections class which is reverseOrder().

Here’s the example,

import java.util.Arrays;
import java.util.Collections;

public class ArraySortAscending
{
   public static void main(String[] args)
   {
      String strAsc[] = {"Bear","Fox","Deer","Cheetah","Anteater","Elephant"}; 

      System.out.println("Before sorting: ");
      for(String string : strAsc)
      {
         System.out.println(string);
      }

      Arrays.sort(strAsc,Collections.reverseOrder());   
      System.out.println("-----------------------------------");
  
      System.out.println("After sorting: ");
      for(String string : strAsc)
      {
         System.out.println(string);
      }
   }
}

Output :

Before sorting:
Bear
Fox
Deer
Cheetah
Anteater
Elephant
———————————–
After sorting:
Fox
Elephant
Deer
Cheetah
Bear
Anteater

 


Now we will use the same example to sort array in ascending order,

import java.util.Arrays;

public class ArrayIntegerDemo 
{
   public static void main(String[] args)
   {
      String arrString[] = {"Bear", "Fox", "Deer", "Cheetah", "Anteater", "Elephant"};

      // sorting array in java in ascending order
      Arrays.sort(arrString);
      System.out.printf("Sorted array java : %s", Arrays.toString(arrString)); 
   }
}

Output :

Sorted array java : [Anteater, Bear, Cheetah, Deer, Elephant, Fox]

 


conclusion:

Well that’s it. This is all about how to sort elements in array in ascending order in java. You can subscribe to my blog if you haven’t.

If you find this post useful share it across social networks.

You May Also Like