Sorting in Java with Arrays.Sort()
In this article, you will be learning about the Arrays.Sort() in Java and how this function can be used for sorting arrays in java.
[toc]
Introduction about Arrays.Sort() in Java
By definition, sorting is the way to arrange elements in order. In computer language, it is the method of arranging the elements of a list or array in a certain order.
It can either be ascending or descending order. The numerical and lexicographical (alphabetical) order is the widely used order to sort. We will be learning about sort() in detail.
So basically sort() method is a java.util.Arrays class method.
Java.util.Arrays
The java.util.Arrays.sort (Object[] a, int fromIndex, int toIndex) method sorts the given range of the given array of objects into ascending order, based on the natural ordering of its elements. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive.
Syntax
public static void sort(Object[] a, int fromIndex, int toIndex)
- a – the array which needs to be sorted.
- fromIndex − This is the index of the first element (inclusive) to be sorted.
- toIndex − This is the index of the last element (exclusive) to be sorted.
Sort Array of Integers in ascending order
Let us see how to sort an array of integers in ascending order through a program.
Input
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] arr = {114,111, 116,113, 115, 112 }; Arrays.sort(arr); System.out.printf("Ascending order of the arr[] : %s", Arrays.toString(arr)); } }
Output:
Ascending order of the arr [] : [111,112,113,114,115,116]
Sort Array of Integers in descending order
Let us see how to sort an array of integers in descending order through a program.
Input
import java.util.Arrays; import java.util.Collections; public class Main { public static void main(String[] args) { Integer[] arr = {114,111, 116,113, 115, 112 }; Arrays.sort(arr, Collections.reverseOrder()); System.out.printf("Descending order of arr[] : %s", Arrays.toString(arr)); } }
Output:
Descending order of arr [] : [116,115,114,113,112,111]
Comparator Interface for Sorting Arrays in Java
Comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of two different classes.
Syntax:
public int compare(Object obj1, Object obj2):
How does Collections.Sort() work?
While using the Sort method, it calls the Compare method of the classes of which it is sorting. To compare the two elements, it checks“Which is greater?”. In turn, the Compare method returns boolean values such as -1, 0, or 1 to say if it is less than, equal, or greater to the other. With this data in hand, it performs the necessary action for the sort ().
Input
import java.util.*; import java.lang.*; import java.io.*; class Student { int registerno; String studentname, studentaddress; public Student(int registerno, String studentname, String address) { this.registerno = registerno; this.studentname = studentname; this.studentaddress = studentaddress; } public String toString() { return this.registerno + " " + this.studentname + " " + this.studentaddress; } } class Sortbyroll implements Comparator<Student> { public int compare(Student a, Student b) { return a.registerno - b.registerno; } } class Sortbyname implements Comparator<Student> { public int compare(Student a, Student b) { return a.studentname.compareTo(b.studentname); } } public class Main { public static void main (String[] args) { ArrayList<Student> ar = new ArrayList<Student>(); ar.add(new Student(112, "yyyy", "United Kingodm")); ar.add(new Student(111, "xxxx", "India")); ar.add(new Student(113, "zzzz", "China")); System.out.println("Unsorted"); for (int i=0; i<ar.size(); i++) System.out.println(ar.get(i)); Collections.sort(ar, new Sortbyroll()); System.out.println("\nSorted by registerno"); for (int i=0; i<ar.size(); i++) System.out.println(ar.get(i)); Collections.sort(ar, new Sortbyname()); System.out.println("\nSorted by name"); for (int i=0; i<ar.size(); i++) System.out.println(ar.get(i)); } }
Output:
Unsorted 112 yyyy United Kingodm 111 xxxx India 113 zzzz China Sorted by registerno 112 yyyy United Kingodm 113 zzzz China 111 xxxx India Sorted by name 111 xxxx India 112 yyyy United Kingodm 113 zzzz China

Tag:Arrays.sort(), Java