Table of content

How to sort an array in Java

Sorting is arranging data in a specific order. The order can be ascending or descending. Alphabets and numbers can be sorted in ascending or descending order.

Sorting an array using the sort() method

In java.util package, the Arrays class provides sort() method to sort an array in ascending order. The method can be invoked directly using the class name. It accepts an array of data type int, float, double, long, char, byte.

The syntax of the sort() method is given below

public static void sort(int[] arry)

Let us consider an example of array exmp=[281, 59, 358, 81, 34, 985, 736]. The sort() method returns the ascending order of the array exmp.

import java.util.Arrays;
public class Srtarry {
  public static void main(String args[]) {
    int[] exmp = {281, 59, 358, 81, 34, 985, 736 };
    System.out.println("Before sorting:\n" + Arrays.toString(exmp));
    Arrays.sort(exmp);
    System.out.println("\n");
    System.out.println("After sorting:\n" + Arrays.toString(exmp));
  }
}

The output is

Before sorting:
[281, 59, 358, 81, 34, 985, 736]

After sorting:
[34, 59, 81, 281, 358, 736, 985]

The sort() method can be used to sort an array of strings in ascending order. Let us consider a String array arr = {"Python", "JavaScript", "Ruby", "C++", "HTML");

The example below illustrates the sorting of string using the sort() method.

import java.util.Arrays;
public class Stringsort {
  public static void main(String args[]) {
    String arr[] = { "Python", "JavaScript", "Ruby", "C++", "HTML" };
    System.out.println("Before sorting:\n" + Arrays.toString(arr));
    Arrays.sort(arr);
    System.out.println("\n");
    System.out.println("After sorting:\n" + Arrays.toString(arr));
  }
}

The output

Before sorting:
[Python, JavaScript, Ruby, C++, HTML]

After sorting:
[C++, HTML, JavaScript, Python, Ruby]

Sorting an array using the reverseOrder() method

In Java, the Collections class contains reverseOrder() method that sorts the array in reverse order. It is a static method and can be invoked directly by using the class name. The reverseOrder() method returns a comparator. A comparator interface is used to sort objects in Java.

The syntax for the same is given below:

public static<T> Comparator<T>reverseOrder()

Let us consider an example of array ar = {421, 25, 789, 354, 60, 1010}. The reverseOrder() method returns the descending order of the array ar.

import java.util.Arrays;
import java.util.Collections;
public class reverseSort {
  public static void main(String args[]) {
    Integer ar[] = {421, 25, 789, 354, 60, 1010 };
    System.out.println("Before sorting:\n" + Arrays.toString(ar));
    Arrays.sort(ar, Collections.reverseOrder());
    System.out.println("\n");
    System.out.println("After sorting:\n" + Arrays.toString(ar));

  }
}

The output

Before sorting:
[421, 25, 789, 354, 60, 1010]

After sorting:
[1010, 789, 421, 354, 60, 25]

The reverseOrder() method can be used to reverse a string of array. Let us consider an array of strings str = {"Jeep", "KIA", "Renault", "Hyundia", "Volkswagen"}.

The program below sorts the array str in descending order.

import java.util.Arrays;
import java.util.Collections;
public class reverseSortAlp {
  public static void main(String args[]) {
    String str[] = {"Jeep", "KIA", "Renault", "Hyundia", "Volkswagen"};
    System.out.println("Before sorting:\n" + Arrays.toString(str));
    Arrays.sort(str, Collections.reverseOrder());
    System.out.println("\n");
    System.out.println("After sorting:\n" + Arrays.toString(str));
  }
}

The output is as follows:

Before sorting:
[Jeep, KIA, Renault, Hyundia, Volkswagen]

After sorting:
[Volkswagen, Renault, KIA, Jeep, Hyundia]

Sorting an array using for loop

We can sort an array using for loop in both ascending as well as descending order. The for loop can be used to sort both numbers and strings in ascending or descending order.

Let us consider an example of array num = { 55, 28, 121, 255, -23, 99, 76, -56 }. The array is sorted in ascending order as follows:

import java.util.*;
public class loopSort {
  public static void main(String args[]) {
    int num[] = {55, 28, 121, 255, -23, 99, 76, -56};
    System.out.println("Before sorting:\n" + Arrays.toString(num));
    System.out.println("\n");
    System.out.println("After sorting:");
    for (int i = 0; i < num.length; i++) {
      for (int j = i + 1; j < num.length; j++) {
        int tmp = 0;
        if (num[i] > num[j]) {
          tmp = num[i];
          num[i] = num[j];
          num[j] = tmp;
        }
      }

    }
    System.out.println(Arrays.toString(num));
  }
}

The output is as follows:

Before sorting:
[55, 28, 121, 255, -23, 99, 76, -56]

After sorting:
[-56, -23, 28, 55, 76, 99, 121, 255]
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions