Java Array Sort

The Java Array Sort Method is one of the Java Array Methods, which is to sort the user-specified array in Ascending or Descending order. In this article, we will show how to sort the Java Byte Array, Object Array, Integer Array, Char Array, Long Array, Double Array, Float Array, Short Array in both Ascending Order and Descending Order. The basic syntax of the Arrays.sort in Java Programming language is as shown below.

Java Array sort Method syntax

The Java Programming Language provides eighteen different Java Array sort methods to perform sorting on the Java Array. The following method will accept the Byte Array as the parameter and sort the Byte array in Ascending Order.

public static void sort(byte[] anByteArray); 

//In order to use in program
Arrays.sort(byte[] anByteArray);

The following Java Arrays.sort method will accept the Byte Array as the first argument. The starting index position (fromIndex) where the sorting will begin as the second parameter (integer value), and last index position (toIndex) where the sorting will end as the third argument. The Arrays sort method will sort the array elements starting from the fromIndex up to toIndex but not included.

public static void sort(byte[] anByteArray, int fromIndex, int toIndex); 

//In order to use in program
Arrays.sort(byte[] anByteArray, int fromIndex, int toIndex);

Below Java Arrays.sort method will accept the short Array as the parameter and sort the Short array in Ascending.

public static void sort(short[] anShortArray); 

//In order to use in program
Arrays.sort(short[] anShortArray);

It accepts the Short Array as the first argument, starting index position (fromIndex) as the second parameter, and last index position (toIndex) as the third argument.

public static void sort(byte[] anByteArray, int fromIndex, int toIndex); 

//In order to use in program
Arrays.sort(byte[] anByteArray, int fromIndex, int toIndex);

It will accept the Character Array as the parameter and sort the char array in Ascending.

public static void sort(char[] anCharArray); 

//In order to use in program
Arrays.sort(char[] anCharArray);

This Java Arrays.sort method accepts the Character Array as the first argument, starting index position (fromIndex), and last index position (toIndex) as the third argument.

public static void sort(char[] anCharArray, int fromIndex, int toIndex); 

//In order to use in program
Arrays.sort(char[] anCharArray, int fromIndex, int toIndex);

It accepts the Integer Array as the parameter and sorts the Int array in Ascending order.

public static void sort(int[] anIntArray); 

//In order to use in program
Arrays.sort(int[] anIntArray);

This Java Arrays.sort method will accept the Integer Array as the first argument, the starting index position (fromIndex), where the sorting will begin as the second parameter. And last index position (toIndex), where the sorting will end as the third argument.

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

//In order to use in program
Arrays.sort(int[] anIntArray, int fromIndex, int toIndex);

The following method will accept the Long Array as the parameter and sort the Long array in Ascending order.

public static void sort(long[] anLongArray); 

//In order to use in program
Arrays.sort(long[] anLongArray);

It accepts the Long Array as the first argument, fromIndex as the second parameter, and toIndex as the third argument.

public static void sort(long[] anLongArray, int fromIndex, int toIndex); 

//In order to use in program
Arrays.sort(long[] anLongArray, int fromIndex, int toIndex);

The Java Arrays sort method will accept the Double Array as the parameter and sort the Double array in Ascending order.

public static void sort(double[] anDoubleArray); 

//In order to use in program
Arrays.sort(double[] anDoubleArray);

The following method will accept the Double Array, starting index position (fromIndex) as the second parameter, and last index position (toIndex) as the third argument.

public static void sort(double[] anDoubleArray, int fromIndex, int toIndex); 

//In order to use in program
Arrays.sort(double[] anDoubleArray, int fromIndex, int toIndex);

This Java sort method will accept the Floating-point Array as the parameter and sort the Float array in Ascending order.

public static void sort(float[] anFloatArray); 

//In order to use in program
Arrays.sort(float[] anFloatArray);

It accepts the Floating-point Array, starting index position (fromIndex), and last index position (toIndex).

public static void sort(float[] anFloatArray, int fromIndex, int toIndex); 

//In order to use in program
Arrays.sort(float[] anFloatArray, int fromIndex, int toIndex);

This array sort method accepts the Object Array as the parameter. It sorts the Object array in the order induced by the specified Comparator.

public static void sort(T[] Array, Comparator<? super T> cmp); 

//In order to use in program
Arrays.sort(T[] Array, Comparator<? super T> cmp);

The Java sort method accepts the Object Array, starting index position (fromIndex), last index position (toIndex), and Comparator (specify the Order) as the fourth argument.

public static void sort(T[] Array, int fromIndex, int toIndex, Comparator<? super T> cmp); 

//In order to use in program
Arrays.sort(T[] Array, int fromIndex, int toIndex, Comparator<? super T> cmp);
  • fromIndex: Please specify the starting index position. It is the index position where the Sorting will begin.
  • toIndex: Please specify the ending index position. Java Arrays.sort method will sort up to this index position. However, it will not include the element at this position (toIndex).

Sorting Byte Array using Java Array Sort Method

The Java Arrays.sort method will Sort the elements of an Array starting from the FromtoIndex up to toIndex but not included. In this Java program, we declared the byte array with random array elements. Then we will Sort the Java array elements in Ascending Order.

// Sorting Byte Array using Java Arrays.sort Method
package ArrayMethods;
import java.util.Arrays;

public class ByteSort {
	public static void main(String[] args) {
		byte[] byteArray = {20, 8, 4, 18, 45}; 
		byte[] bitArray = {10, 25, 8, 19, 16, 5}; 
		
		//Sorting Array in Ascending Order
		Arrays.sort(byteArray);
	
		//Printing the Output
		System.out.println("Sorting Byte Array:");
		arrayPrint(byteArray);
		
		//Sorting Array using Indexes
		Arrays.sort(bitArray, 1, 5);
		
		//Printing the Output
		System.out.println("Sorting Byte Array Using Index:");
		arrayPrint(bitArray);
	}
	public static void arrayPrint(byte[] anByteArray) {
		for (byte Number: anByteArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Java Array Sort 1

Within this Java Arrays.sort example, we declared two-byte Arrays. Next, we assigned some random values as the array elements using the following statements.

byte[] byteArray = {20, 8, 4, 18, 45}; 
byte[] bitArray = {10, 25, 8, 19, 16, 5};

The following statement will call the public static void sort(byte[] anByteArray) method to sort the byte array in Ascending Order.

Arrays.sort(byteArray);

The following statement is to print the Byte array elements to the output.

arrayPrint(byteArray);

From the below code snippet, we used the Java For Loop to iterate the Byte Array. Then we are printing every array element using the System.out.println statement.

public static void arrayPrint(byte[] anByteArray) {
	for (byte Number: anByteArray) {
		System.out.println("Array Elelment = " + Number);
	}
}

It calls the public static void sort(byte[] anByteArray, int fromIndex, int toIndex) method to sort the byte array from index position 1 to position 4 in Ascending Order.

Arrays.sort(bitArray, 1, 5);

Sorting Short Array using Java Array Sort Method

In this Arrays.sort Java program, we declared the Short array with random array elements. Then we will Sort the short array elements in Ascending Order.

// Sorting Short Array using Java Arrays.sort Method
package ArrayMethods;
import java.util.Arrays;
public class ShortSort {
	public static void main(String[] args) {
		short[] shrArray = {9, 6, 2, 7, 5}; 
		short[] ShortArray = {10, 5, 8, 4, 6}; 	
	
		//Sorting Array in Ascending Order
		Arrays.sort(shrArray);
		System.out.println("Sorting Short Array:");
		arrayPrint(shrArray);
		
		//Sorting Array using Indexes
		Arrays.sort(ShortArray, 0, 4);
		System.out.println("Sorting Short Array Using Index:");
		arrayPrint(ShortArray);
	}
	public static void arrayPrint(short[] anShortArray) {
		for (short Number: anShortArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Sorting Short Array:
Array Elelment = 2
Array Elelment = 5
Array Elelment = 6
Array Elelment = 7
Array Elelment = 9
Sorting Short Array Using Index:
Array Elelment = 4
Array Elelment = 5
Array Elelment = 8
Array Elelment = 10
Array Elelment = 6

It calls the public static void sort(short[] anShortArray) method to sort the Short array in Ascending Order.

Arrays.sort(shrArray);

It calls the public static void sort(short[] anShortArray, int fromIndex, int toIndex) method to sort the Short array from index position 1 to position 4 in Ascending Order.

Arrays.sort(ShortArray, 0, 4);

Sorting Intger Array using Java Array Sort Method

In this Java program, we declared the Integer array with random array elements. Then we will Sort the Int array elements in Ascending Order.

// Sorting Integer Array using Java Arrays.sort Method
package ArrayMethods;
import java.util.Arrays;
public class IntSort {
	public static void main(String[] args) {
		int[] IntArray = {98, 64, 7, 78, 35}; 
		int[] IntegerArray = {10, 25, 8, 19, 16}; 
		
		//Sorting Array in Ascending Order
		Arrays.sort(IntArray);
		System.out.println("Sorting Integer Array:");
		arrayPrint(IntArray);
		
		//Sorting Array using Indexes
		Arrays.sort(IntegerArray, 0, 3);
		System.out.println("Sorting Integer Array Using Index:");
		arrayPrint(IntegerArray);
	}
	public static void arrayPrint(int[] anIntArray) {
		for (int Number: anIntArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Sorting Integer Array:
Array Elelment = 7
Array Elelment = 35
Array Elelment = 64
Array Elelment = 78
Array Elelment = 98
Sorting Integer Array Using Index:
Array Elelment = 8
Array Elelment = 10
Array Elelment = 25
Array Elelment = 19
Array Elelment = 16

The following statement will call the public static void sort(int[] anIntegerArray) method to sort the integer array in Ascending Order.

Arrays.sort(IntArray);

It calls the public static void sort(int[] anIntegerArray, int fromIndex, int toIndex) method to sort the integer array from index position 1 to position 4 in Ascending Order.

Arrays.sort(IntegerArray, 0, 3);

Sorting Long Array using Java Array Sort Method

In this Java program, we declared the Long array with random array elements. Then we will Sort the Java Long array elements in Ascending Order.

// Sorting Long Array using Java Arrays.sort Method
package ArrayMethods;
import java.util.Arrays;

public class LongSort {
	public static void main(String[] args) {
		long[] LngArray = {198, 164, 17, 178, 135}; 
		long[] LongArray = {100, 250, 80, 190, 160}; 
		
		//Sorting Array in Ascending Order
		Arrays.sort(LngArray);
		System.out.println("Sorting Long Array:");
		arrayPrint(LngArray);
		
		//Sorting Array using Indexes
		Arrays.sort(LongArray, 1, 4);
		System.out.println("Sorting Long Array Using Index:");
		arrayPrint(LongArray);
	}
	public static void arrayPrint(long[] anLongArray) {
		for (long Number: anLongArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Sorting Long Array:
Array Elelment = 17
Array Elelment = 135
Array Elelment = 164
Array Elelment = 178
Array Elelment = 198
Sorting Long Array Using Index:
Array Elelment = 100
Array Elelment = 80
Array Elelment = 190
Array Elelment = 250
Array Elelment = 160

It calls the public static void sort(long[] anLongArray) method to sort the Long Array in Ascending Order.

Arrays.sort(LngArray);

It calls the public static void sort(long[] anLongArray, int fromIndex, int toIndex) to sort the Long array from index position 1 to position 3 in Ascending Order.

Arrays.sort(LongArray, 1, 4);

Sorting Double Array using Array Sort Method

In this Java program, we declared the Double array with random array elements. Then we will Sort the Java Double array elements in Ascending Order.

// Sorting Double Array using Java Arrays.sort Method
package ArrayMethods;
import java.util.Arrays;

public class DoubleSort {
	public static void main(String[] args) {
		double[] DoubArray = {98.68, 64.542, 7.98, 78.45, 35.76}; 
		double[] DoubleArray = {10.25, 25.9865, 8.485, 19.356, 16.489}; 
		
		//Sorting Array in Ascending Order
		Arrays.sort(DoubArray);
		System.out.println("Sorting Double Array:");
		arrayPrint(DoubArray);
		
		//Sorting Array using Indexes
		Arrays.sort(DoubleArray, 0, 3);
		System.out.println("Sorting Double Array Using Index:");
		arrayPrint(DoubleArray);
	}
	public static void arrayPrint(double[] anDoubleArray) {
		for (double Number: anDoubleArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Sorting Double Array:
Array Elelment = 7.98
Array Elelment = 35.76
Array Elelment = 64.542
Array Elelment = 78.45
Array Elelment = 98.68
Sorting Double Array Using Index:
Array Elelment = 8.485
Array Elelment = 10.25
Array Elelment = 25.9865
Array Elelment = 19.356
Array Elelment = 16.489

The following statement will call the public static void sort(double[] anDoubleArray) method to sort the Double array in Ascending Order.

Arrays.sort(DoubArray);

It calls the public static void sort(double[] anDoubleArray, int fromIndex, int toIndex) method to sort the Double array from index position 0 to position 2 in Ascending Order.

Arrays.sort(DoubleArray, 0, 3);

Sorting Float Array using Array Sort Method

In this Java program, we declared the Float array with random array elements. Then we will Sort the Java Float array elements in Ascending Order.

// Sorting Float Array using Java Arrays.sort Method
package ArrayMethods;
import java.util.Arrays;

public class FloatSort {
	public static void main(String[] args) {
		float[] floArray = {8.68f, 5.54f, 1.98f, 12.45f, 3.76f}; 
		float[] floatArray = {10.25f, 25.98f, 8.4f, 13.3f, 16.48f}; 
		
		//Sorting Array in Ascending Order
		Arrays.sort(floArray);
		System.out.println("Sorting Float Array:");
		arrayPrint(floArray);
		
		//Sorting Array using Indexes
		Arrays.sort(floatArray, 0, 4);
		System.out.println("Sorting Float Array Using Index:");
		arrayPrint(floatArray);
	}
	public static void arrayPrint(float[] anfloatArray) {
		for (float Number: anfloatArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Sorting Float Array:
Array Elelment = 1.98
Array Elelment = 3.76
Array Elelment = 5.54
Array Elelment = 8.68
Array Elelment = 12.45
Sorting Float Array Using Index:
Array Elelment = 8.4
Array Elelment = 10.25
Array Elelment = 13.3
Array Elelment = 25.98
Array Elelment = 16.48

The following statement will call the public static void sort(float[] anFloatArray) method to sort the Float array in Ascending Order.

Arrays.sort(floArray);

It calls the public static void sort(float[] anFloatArray, int fromIndex, int toIndex) method to sort the Float array from index position 0 to position 3 in Ascending Order.

Arrays.sort(floatArray, 0, 4);

Sorting Char Array using Java Array Sort Method

In this Java program, we declared the Character array with random array elements. Then we will Sort the Char array elements in Ascending Order.

// Sorting Char Array using Java Arrays.sort Method
package ArrayMethods;
import java.util.Arrays;

public class CharSort {
	public static void main(String[] args) {
		char[] CharArray = {'g', 'a', 't', 'e'}; 
		char[] CharacterArray = {'g', 'a', 't', 'e', 'w', 'a', 'y'}; 
		
		//Sorting Array in Ascending Order
		Arrays.sort(CharArray);
		System.out.println("Sorting Character Array:");
		arrayPrint(CharArray);
		
		//Sorting Array using Indexes
		Arrays.sort(CharacterArray, 2, 6);
		System.out.println("Sorting Character Array Using Index:");
		arrayPrint(CharacterArray);
	}
	public static void arrayPrint(char[] anCharacterArray) {
		for (char Number: anCharacterArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Sorting Character Array:
Array Elelment = a
Array Elelment = e
Array Elelment = g
Array Elelment = t
Sorting Character Array Using Index:
Array Elelment = g
Array Elelment = a
Array Elelment = a
Array Elelment = e
Array Elelment = t
Array Elelment = w
Array Elelment = y

It calls the public static void sort(char [] anCharArray) method to sort the Char array in Ascending Order.

Arrays.sort(CharArray);

It calls the public static void sort(char[] anCharArray, int fromIndex, int toIndex) to sort the Char array from index position 2 to position 5 in Ascending Order.

Arrays.sort(CharacterArray, 2, 6);

Sorting Object Array using Array Sort Method

In this Java program, we declared the String array with random array elements. Then we will Sort the string object array elements in Ascending Order.

// Sorting Object Array using Java Arrays.sort Method
package ArrayMethods;
import java.util.Arrays;

public class ObjectSort {
	public static void main(String[] args) {
		String[] strArray = {"Apple", "Orange", "Grape", "Banana", "Strawberry","Cherry", "Kiwi"};
		String[] stringArray = {"India", "UK", "USA", "Australia", "Japan","China", "Korea"};
		
		//Sorting Array in Ascending Order
		Arrays.sort(strArray);
		System.out.println("Sorting Object Array:");
		arrayPrint(strArray);
		
		//Sorting Array using Indexes
		Arrays.sort(stringArray, 2, 6);
		System.out.println("Sorting Object Array Using Index:");
		arrayPrint(stringArray);
	}
	public static void arrayPrint(Object[] anObjectArray) {
		for (Object Number: anObjectArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Sorting Object Array:
Array Elelment = Apple
Array Elelment = Banana
Array Elelment = Cherry
Array Elelment = Grape
Array Elelment = Kiwi
Array Elelment = Orange
Array Elelment = Strawberry
Sorting Object Array Using Index:
Array Elelment = India
Array Elelment = UK
Array Elelment = Australia
Array Elelment = China
Array Elelment = Japan
Array Elelment = USA
Array Elelment = Korea

Within this Array sort program, the following statement will call the public static void sort(Object[] anObjectArray) method to sort the Object array in Ascending Order.

Arrays.sort(strArray);

It calls the public static void sort(Object[] anObjectArray, int fromIndex, int toIndex) to sort the Object array from index position 2 to position 5 in Ascending Order.

Arrays.sort(stringArray, 2, 6);

Java Array Sort Method to Sort Object Array in Descending Order

In this Java program, we declared the String, Object arrays with random array elements. Then we will Sort the short array elements in Descending Order.

// Sorting Array in Descending Order using Java Arrays.sort Method
package ArrayMethods;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class TestSort {
	public static void main(String[] args) {
		String[] stringArray = {"India", "UK", "USA", "Australia", "Japan","China", "Korea"};
		Object[] elements = {10, 5, 17, 2, 9, 11};
		//Sorting Array in Ascending Order
		Comparator<String> cmp = Collections.reverseOrder();
		Arrays.sort(stringArray, cmp);
		System.out.println("Sorting Object Array:");
		arrayPrint(stringArray);
		
		//Sorting Array using Indexes
		Comparator<Object> cms = Collections.reverseOrder();
		Arrays.sort(elements, 1, 5, cms);
		System.out.println("Sorting Object Array Using Index:");
		arrayPrint(elements);
	}
	public static void arrayPrint(Object[] anIntArray) {
		for (Object Number: anIntArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Sorting Object Array:
Array Elelment = USA
Array Elelment = UK
Array Elelment = Korea
Array Elelment = Japan
Array Elelment = India
Array Elelment = China
Array Elelment = Australia
Sorting Object Array Using Index:
Array Elelment = 10
Array Elelment = 17
Array Elelment = 9
Array Elelment = 5
Array Elelment = 2
Array Elelment = 11

It will create the Comparator for String Object. On the right side, we call the reverseOrder method and assign the Order (descending order) to Comparator

Comparator<String> cmp = Collections.reverseOrder();

The following statement will call the public static void sort(T[] Array, Comparator<? super T> cmp) method to sort the String object array in Descending Order.

Arrays.sort(stringArray, cmp);

It will create the Comparator for Object. On the right-hand side, we call the reverseOrder method and assign the Order (descending order) to Comparator.

Comparator<Object> cms = Collections.reverseOrder();

The following Array sort statement will call the public static void sort(T[] Array, int fromIndex, int toIndex, Comparator cmp) method to sort the Object array from index position 1 to position 5 in Descending Order.

Arrays.sort(elements, 1, 5, cms);