Table of Content

How to sort elements in Object Array using Index in Java?


Declaration :

Explanation :

Purpose The java.util.Arrays.sort(Object[] a int fromIndex int toIndex) method sorts the specified range of the specified array of objects into ascending order according to the natural ordering of its elements. The range to be sorted extends from index fromIndex inclusive to index toIndex exclusive.
Parameters a ===> This is the array 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.
Return Value This method does not return any value.
Exception IllegalArgumentException — if fromIndex > toIndex
,ArrayIndexOutOfBoundsException — if fromIndex < 0 or toIndex > a.length
,ClassCastException — if the array contains elements that are not mutually comparable (for example strings and integers)

Java Program : Example

Below example will explain java.util.Arrays.sort() method.

Output of Program :