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.
DeclarationFollowing is the declaration for java.util.Arrays.sort() method
public static void sort(Object[] a, int fromIndex, int toIndex)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.
This method does not return any value.
ExceptionIllegalArgumentException − 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)
The following example shows the usage of java.util.Arrays.sort() method.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // initializing unsorted Object array Object ob[] = {27, 11, 5, 44}; // let us print all the elements available in list for (Object number : ob) { System.out.println("Number = " + number); } // sorting array from index 1 to 3 Arrays.sort(ob, 1, 3); // let us print all the elements available in list System.out.println("Object array with some sorted values(1 to 3) is:"); for (Object number : ob) { System.out.println("Number = " + number); } } }
Let us compile and run the above program, this will produce the following result −
Number = 27 Number = 11 Number = 5 Number = 44 Object array with some sorted values(1 to 3) is: Number = 27 Number = 5 Number = 11 Number = 44
java_util_arrays.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4