Java sort array

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

java sort array

We can use java arrays.sort method to sort elements in an array. Either it ascending or descending order. arrays.sort method is in java.util.Arrays class.

Also read – java sort string array alphabetically

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.

Here’s an example on sorting array in ascending order 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(Arrays.toString(arrNum));
   }
}

Output :

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


Now let’s learn how to sort a subarray in java using arrays.sort.

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

Output :

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


Similarly, using the same example we can sort in descending order using arrays.sort.

Also read – java sort both primitive and object array

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

Output :

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


In alphabetical order

Now we will see how to print strings in descending order. So in order to print strings 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.

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"};
      // ascending order
      Arrays.sort(arrString);
      System.out.printf(Arrays.toString(arrString)); 
   }
}

Output :

[Anteater, Bear, Cheetah, Deer, Elephant, Fox]


Sorting array – according particular criteria

Using array.sort java comparator we can sort an array according to particular criteria. Here Comparator interface is used,

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 tutee[] = { 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 < tutee.length; a++) 
         System.out.println(tutee[a]);

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

      System.out.println("\nSorted array - sorted by ID : "); 
      for(int a = 0; a < tutee.length; a++) 
         System.out.println(tutee[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

Related Posts