java.util.Arrays sort(Object[] a, int fromIndex, int toIndex)

Description

On this document we will be showing a java example on how to use the sort(Object[] a, int fromIndex, int toIndex) method of Arrays Class. Basically the 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. (If fromIndex==toIndex, the range to be sorted is empty.) All elements in this range must implement the Comparable interface. Furthermore, all elements in this range must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

The sort() method is overloaded in such a way that each primitive data type is handled. On this example we will be showing the sorting of Objects at specified index range.

Notes:

  • This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.
  • This method throws IllegalArgumentException if fromIndex is greather than toIndex
  • This method will also throws an ArrayIndexOutOfBoundsException if fromIndex is less than 0 or toIndex is greater than a.length
  • It will also throw ClassCastException if the array contains elements that are not mutually comparable (for example, strings and integers).
  • This method will yield the same result as sort(Object[] a) method on such cases that the fromIndex is 0 and endIndex is equal to length. In short Arrays.sort(Object[] a) is equals to Arrays.sort(Object[] a, 0,a.length).

Method Syntax

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

Method Argument

Data Type Parameter Description
Object[] a the array to be sorted
int fromIndex the index of the first element, inclusive, to be sorted
int toIndex the index of the first element, inclusive, to be sorted

Method Returns

The sort(Object[] a, int fromIndex, int toIndex) method returns void.

Compatibility

Requires Java 1.2 and up

Java Arrays sort(Object[] a, int fromIndex, int toIndex) Example

Below is a java code demonstrates the use of sort(Object[] a, int fromIndex, int toIndex) method of Arrays class. The example presented might be simple however it shows the behavior of the sort(Object[] a, int fromIndex, int toIndex) method.

package com.javatutorialhq.java.examples;

import java.util.Arrays;

/*
 * A java example source code to demonstrate
 * the use of sort(Object[] a, int fromIndex, int toIndex) 
 * method of Arrays class
 */

public class ArraysSortObjectRangeExample {

	public static void main(String[] args) {

		// initialize a new Object array
		Object[] a = new Object[]{"Test",
				"String",
				"123",
				"Ab",
				"AB",
				"A2",
				"A1"};
		
		// print the contents of array of Objects
		System.out.println("Original:"+Arrays.toString(a));
		
		// sort our array of Object from index 2 to 7 
		Arrays.sort(a,2,7);
		
		// print the sorted array of Object
		System.out.println("Sorted:"+Arrays.toString(a));

	}
}

The above java example source code demonstrates the use of sort(Object[] a, int fromIndex, int toIndex) method of Arrays class. We simply declare a new Object array and we printed the values using the toString() method of Arrays class. Then we sorted the Object array from index 0 to 6 using the sort() method and the sorted values were also printed the same way as what we did on the original/unsorted array.

Sample Output

Below is the sample output when you run the above example.

Arrays sort(Object[] a, int fromIndex, int toIndex) example output