by BehindJava

How to sort an array in Java

Home » datastructures » How to sort an array in Java

In this tutorial, we are going to learn about sort an array using Arrays class in java.

We have the JDK that contains the arrays class which has method that sort arrays.

So, let’s take a look at the arrays Java doc. this class contains various methods for manipulating arrays such as sorting.

So, these all methods are for primitive types and to sort objects at the runtime you’d implement the comparable interface.

import java.util.Arrays;

public class ArraySort {
	public static void main(String[] args) {
		int[] intArray = { 20, 35, -15, 7, 55, 1, -22 };

		Arrays.parallelSort(intArray);

		for (int i = 0; i < intArray.length; i++) {
			System.out.print(intArray[i]+ " ");
		}
	}
}

Output:

-22 -15 1 7 20 35 55