Signup/Sign In
PUBLISHED ON: JANUARY 22, 2021

Java Arrays sort() Method

In this tutorial, we will learn about sort() method of the Arrays class in Java. This method accepts an array and sorts it in ascending order. We can extend the feature of this method by providing a range of an array with startIndex and endIndex. Let's see its syntax and examples.

Syntax

static void	sort(datatype[] array)

List of Overloading Methods of sort() Method

This table contains all the overloaded variants of sort() method.

Method Description

static void sort(byte[] array)

This method sorts the specified array into ascending numerical order.

static void sort(byte[] array, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending order.

static void sort(char[] array)

This method sorts the specified array into ascending numerical order.

static void sort(char[] array, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending order.

static void sort(double[] array)

This method sorts the specified array into ascending numerical order.

static void sort(double[] array, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending order.

static void sort(float[] array)

This method sorts the specified array into ascending numerical order.

static void sort(float[] array, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending order.

static void sort(int[] array)

This method sorts the specified array into ascending numerical order.

static void sort(int[] array, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending order.

static void sort(long[] array)

This method sorts the specified array into ascending numerical order.

static void sort(long[] array, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending order.

static void sort(short[] array)

This method sorts the specified array into ascending numerical order.

static void sort(short[] array, int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending order.

static void sort(Object[] array)

This method sorts the specified array of objects into ascending order, according to the natural ordering of its elements.

static void sort(Object[] array, int fromIndex, int toIndex)

This method sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.

static <T> void sort?(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)

This method sorts the specified range of the specified array of objects according to the order induced by the specified comparator.

static <T> void sort?(T[] a, Comparator<? super T> c)

This method sorts the specified array of objects according to the order induced by the specified comparator.

Example of sort() method

In the following example, we can see sort() method is accepting arr which is unsorted initially and this method sorts it in ascending order.

import java.util.Arrays;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		int arr[] = {10, 8, 12, 1, 12, 5, 16, 4, 10, 3, 14, 11, 20, 17, 18};
		System.out.println("Array Before Sorting ");
		for(int num:arr)
		{
			System.out.print(num+" ");
		}
		Arrays.sort(arr);
		System.out.println("\nArray After Sorting ");
		for(int num:arr)
		{
			System.out.print(num+" ");
		}
	} 
}


Array Before Sorting
10 8 12 1 12 5 16 4 10 3 14 11 20 17 18
Array After Sorting
1 3 4 5 8 10 10 11 12 12 14 16 17 18 20

Example of sort() method with an array of different datatype and different range

In the following program, we have illustrated how to use sort() method on different datatypes and for reference, we mentioned an array of int and an array of float. Similarly, we can also see how to provide a range of an array so that on the particular range only sorting will be performed.

import java.util.Arrays;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		int array1[] = {10, 8, 12, 1, 12, 5, 16, 4, 10, 3, 14, 11, 20, 17, 18};
		//sorting an array from index 4 to 10 only
		Arrays.sort(array1, 4, 10);
		System.out.println("array1 is sorted from index 4 to 10 ");
		for(int num:array1)
		{
			System.out.print(num+", ");
		}		
		//sorting an array completely
		Arrays.sort(array1);
		System.out.println("\n\narray1 is sorted completely");
		for(int num:array1)
		{
			System.out.print(num+", ");
		}		
		//sorting an array from index 0 to 5 only
		double array2[] = {4.5, 2.6, 11.23, 5.8, 8.9, 10.24, 5.6, 3.2, 1.2, 8.7};
		Arrays.sort(array2, 0, 5);
		System.out.println("\n\narray2 is sorted from index 0 to 5");
		for(double num:array2)
		{
			System.out.print(num+", ");
		}		
		//sorting an array completely
		Arrays.sort(array2);
		System.out.println("\n\narray2 is sorted completely");
		for(double num:array2)
		{
			System.out.print(num+", ");
		}
	} 
}


array1 is sorted from index 4 to 10
10, 8, 12, 1, 3, 4, 5, 10, 12, 16, 14, 11, 20, 17, 18,
array1 is sorted completely
1, 3, 4, 5, 8, 10, 10, 11, 12, 12, 14, 16, 17, 18, 20,
array2 is sorted from index 0 to 5
2.6, 4.5, 5.8, 8.9, 11.23, 10.24, 5.6, 3.2, 1.2, 8.7,
array2 is sorted completely
1.2, 2.6, 3.2, 4.5, 5.6, 5.8, 8.7, 8.9, 10.24, 11.23,

Not only primitive but non-primitive datatypes like an array of objects can also be sorted by using sort() method

Example of sort() method for sorting an array of objects

In the following example, we illustrate how sort() method can be used to sort an array of non-primitive data types like an array of classes.

For example, we have a Student class and its array of objects and we want to sort them based on the roll numbers of students. To define this type of custom sorting we need a comparator.

import java.util.Arrays;
import java.util.Comparator;
class Student
{
	String stud_name;
	int roll_no;	
	Student(String stud_name, int roll_no)
	{
		this.stud_name=stud_name;
		this.roll_no=roll_no;
	}	 
}
class Sortbyroll implements Comparator<Student>
{
    public int compare(Student s1, Student s2)
    {
        return s1.roll_no - s2.roll_no;
    }
}
class StudyTonight{ 
	public static void main(String args[]) 
	{ 
		Student arr[] = new Student[5];
		arr[0] = new Student("KLM",7);
		arr[1] = new Student("PQR",5);
		arr[2] = new Student("GHI",1);
		arr[3] = new Student("ABC",9);
		arr[4] = new Student("MNO",4);
		System.out.println("Array Before Sorting: ");
		for(Student obj:arr)
		{
			System.out.println("Name: "+obj.stud_name+" Roll No:"+obj.roll_no);			 
		}		
		Arrays.sort(arr,new Sortbyroll());
		System.out.println("\nArray After Sorting: ");
		for(Student obj:arr)
		{
			System.out.println("Name: "+obj.stud_name+" Roll No:"+obj.roll_no);			 
		}
	}
	
}


Array Before Sorting:
Name: KLM Roll No:7
Name: PQR Roll No:5
Name: GHI Roll No:1
Name: ABC Roll No:9
Name: MNO Roll No:4

Array After Sorting:
Name: GHI Roll No:1
Name: MNO Roll No:4
Name: PQR Roll No:5
Name: KLM Roll No:7
Name: ABC Roll No:9

Example of Overloading sort() Methods

In the following method, we implemented all the overloadng method of sort() method on various data types like byte, char, int, double, float and short. We also implemented this method on different ranges of arrays by passing startIndex and endIndex.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		byte byteArray[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60}; 
		Arrays.sort(byteArray,0,5);
		System.out.println("Arrays.sort(byteArray,0,5): "+Arrays.toString(byteArray));
		Arrays.sort(byteArray);
		System.out.println("Arrays.sort(byteArray): "+Arrays.toString(byteArray));		
		
		char charArray[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'}; 
		Arrays.sort(charArray,0,5);
		System.out.println("Arrays.sort(charArray,0,5): "+Arrays.toString(charArray));
		Arrays.sort(charArray);
		System.out.println("Arrays.sort(charArray): "+Arrays.toString(charArray));
		
		int intArray[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150};
		Arrays.sort(intArray,0,5);
		System.out.println("Arrays.sort(intArray,0,5): "+Arrays.toString(intArray));
		Arrays.sort(intArray);
		System.out.println("Arrays.sort(intArray): "+Arrays.toString(intArray));
		
		double doubleArray[] = {5.1, 6.2, 7.2, 8.1, 9.4, 10.2, 11.6, 12.96, 13.2, 14.25, 15.6, 16.4, 17.2}; 
		Arrays.sort(doubleArray,0,5);
		System.out.println("Arrays.sort(doubleArray,0,5): "+Arrays.toString(doubleArray));
		Arrays.sort(intArray);
		System.out.println("Arrays.sort(doubleArray): "+Arrays.toString(doubleArray));
		
		float floatArray[] = {1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f}; 
		Arrays.sort(floatArray,0,5);
		System.out.println("Arrays.sort(floatArray,0,5): "+Arrays.toString(floatArray));
		Arrays.sort(floatArray);
		System.out.println("Arrays.sort(floatArray): "+Arrays.toString(floatArray));
		
		short shortArray[] = {2, 4, 6, 8, 10 ,12, 14, 16, 18, 20}; 
		Arrays.sort(shortArray,0,5);
		System.out.println("Arrays.sort(shortArray,0,5): "+Arrays.toString(shortArray));
		Arrays.sort(shortArray);
		System.out.println("Arrays.sort(shortArray): "+Arrays.toString(shortArray));
	}
}


Arrays.sort(byteArray,0,5): [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60]
Arrays.sort(byteArray): [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60]
Arrays.sort(charArray,0,5): [a, b, c, d, e, f, g, h, i, j, k]
Arrays.sort(charArray): [a, b, c, d, e, f, g, h, i, j, k]
Arrays.sort(intArray,0,5): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]
Arrays.sort(intArray): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]
Arrays.sort(doubleArray,0,5): [5.1, 6.2, 7.2, 8.1, 9.4, 10.2, 11.6, 12.96, 13.2, 14.25, 15.6, 16.4, 17.2]
Arrays.sort(doubleArray): [5.1, 6.2, 7.2, 8.1, 9.4, 10.2, 11.6, 12.96, 13.2, 14.25, 15.6, 16.4, 17.2]
Arrays.sort(floatArray,0,5): [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
Arrays.sort(floatArray): [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
Arrays.sort(shortArray,0,5): [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Arrays.sort(shortArray): [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Conclusion:

In this tutorial, we learned how to use sort() method to sort an array of a given datatype. This data type can be anything like int, float, char, double, long, byte, Object or <T>. To modify sorting based on some data members of the class we can define the comparator function also.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.