Java: Sorting Array in ascending and descending order

In this article, we will discuss how to sort Arrays in both ascending as well as descending order

 

1. Sorting Array in ascending order:

Method Signature:

public static void sort(int[] i);

public static void sort(obj[] a);

 

SortingArrayInAscendingOrder.java

package in.bench.resources.java.arrays.sorting;

import java.util.Arrays;

public class SortingArrayInAscendingOrder {

	public static void main(String[] args) {

		// sample int[] array
		Integer[] intArray = {1975, 2003, 1979, 1992, 1983, 1999, 1987};

		// before sorting
		System.out.println("Integer[] Array - before sorting : ");
		for(Integer iValue : intArray) {
			System.out.println(iValue);
		}

		// sorting int[] array in ascending order
		Arrays.sort(intArray);

		// after sorting
		System.out.println("\nInteger[] Array - after sorting in ascending order : ");
		for(Integer iValue : intArray) {
			System.out.println(iValue);
		}


		// sample String[] array
		String[] strArray = {"Clive", "Kapil", "Allan", "Imran", "Arjuna", "Steve", "Ricky"};

		// before sorting
		System.out.println("\n\n\nString[] Array - before sorting : \n");
		for(String str : strArray) {
			System.out.println(str);
		}

		// sorting String[] array in ascending order
		Arrays.sort(strArray);

		// after sorting
		System.out.println("\nString[] Array - after sorting in ascending order : \n");
		for(String str : strArray) {
			System.out.println(str);
		}
	}
}

Output:

Integer[] Array - before sorting : 
1975
2003
1979
1992
1983
1999
1987

Integer[] Array - after sorting in ascending order : 
1975
1979
1983
1987
1992
1999
2003



String[] Array - before sorting : 

Clive
Kapil
Allan
Imran
Arjuna
Steve
Ricky

String[] Array - after sorting in ascending order : 

Allan
Arjuna
Clive
Imran
Kapil
Ricky
Steve

 

2. Sorting Array in descending order:

Method Signature:

public static void sort(int[] i, Collection.reverseOrder());

public static void sort(obj[] a, Collection.reverseOrder());

 

SortingArrayInDescendingOrder.java

package in.bench.resources.java.arrays.sorting;

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

public class SortingArrayInDescendingOrder {

	public static void main(String[] args) {

		// sample int[] array
		Integer[] intArray = {1975, 2003, 1979, 1992, 1983, 1999, 1987};

		// before sorting
		System.out.println("Integer[] Array - before sorting : ");
		for(Integer iValue : intArray) {
			System.out.println(iValue);
		}

		// sorting int[] array in descending order
		Arrays.sort(intArray, Collections.reverseOrder());

		// after sorting
		System.out.println("\nInteger[] Array - after sorting in descending order : ");
		for(Integer iValue : intArray) {
			System.out.println(iValue);
		}


		// sample String[] array
		String[] strArray = {"Clive", "Kapil", "Allan", "Imran", "Arjuna", "Steve", "Ricky"};

		// before sorting
		System.out.println("\n\n\nString[] Array - before sorting : \n");
		for(String str : strArray) {
			System.out.println(str);
		}

		// sorting String[] array in descending order
		Arrays.sort(strArray, Collections.reverseOrder());

		// after sorting
		System.out.println("\nString[] Array - after sorting in descending order : \n");
		for(String str : strArray) {
			System.out.println(str);
		}
	}
}

Output:

Integer[] Array - before sorting : 
1975
2003
1979
1992
1983
1999
1987

Integer[] Array - after sorting in descending order : 
2003
1999
1992
1987
1983
1979
1975



String[] Array - before sorting : 

Clive
Kapil
Allan
Imran
Arjuna
Steve
Ricky

String[] Array - after sorting in descending order : 

Steve
Ricky
Kapil
Imran
Clive
Arjuna
Allan

 

Note: we need to convert primitive int data-type to Integer wrapper-type by auto-boxing feature introduced in Java 1.5 version, when we pass Comparator object as 2nd argument for reverse order sorting

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
https://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

 

Happy Coding !!
Happy Learning !!