Arrays.sort() in java

In this post we are going to learn Arrays.sort() in java.

We can use arrays.sort() method to sort elements in an array either in ascending or descending order. arrays.sort() method is in package java.util.Arrays.

Syntax:

public static void sort(int[] arr, int fromIndex, int toIndex)

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

sort() method do not return any value.

Here’s the java program to sort array in ascending order,

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.println(Arrays.toString(arrNum));
   }
}

Output :

[5, 8, 14, 23, 85, 100]


Now let’s learn how to sort a subarray of an array,

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.println(Arrays.toString(arrDemo));
   }
}

Output :

[14, 5, 8, 41, 54, 10, 1, 500]


Similarly let’s see java program to sort the array in an ascending order,

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};
      // sort descending order
      Arrays.sort(arrNum, Collections.reverseOrder());
      System.out.println(Arrays.toString(arrNum));
   }
}

Output :

[500, 54, 41, 14, 10, 8, 5, 1]


Sort strings in alphabetical order

Here we need to call the utility method, reverseOrder() of Collections class. Here’s the java program to sort an array of strings in descending order,

Also read – array copyOfRange in java

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:

Arrays.sort() in java


Sorting array – according particular criteria

Using Comparator interface we can sort an array according to particular criteria.

import java.util.Comparator;

// Student class
public class Student 
{
   int ID; 
   String name, city;
   // student constructor 
   public Student(int ID, String name, String city) 
   { 
      this.ID = ID; 
      this.name = name; 
      this.city = city; 
   } 

   // here we are printing student details in main method 
   public String toString() 
   { 
      return this.ID + " " + this.name + " " + this.city; 
   } 
} 

class SortbyID implements Comparator<Student> 
{ 
   // here we are sorting in ascending order of ID 
   public int compare(Student A, Student B) 
   { 
      return A.ID - B.ID; 
   }
}

// main method
import java.util.Arrays;

public class ArraySortDemo 
{
   public static void main(String[] args) 
   {
      Student obj[] = { new Student(103, "Chris", "New Jersey"), 
                          new Student(101, "Kerb", "Texas"), 
                          new Student(102, "John", "Carolina")};

      System.out.println("Unsorted array : "); 
      for(int a = 0; a < obj.length; a++) 
         System.out.println(obj[a]);

      Arrays.sort(obj, new SortbyID());

      System.out.println("\nSorted array - sorted by ID : "); 
      for(int a = 0; a < obj.length; a++) 
         System.out.println(obj[a]); 
   }
}

Output :

Unsorted array :
103 Chris New Jersey
101 Kerb Texas
102 John Carolina

Sorted array – sorted by ID :
101 Kerb Texas
102 John Carolina
103 Chris New Jersey


Also read – arrays in java