Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

Java Arrays.sort() method

Reference for Java Arrays.sort(): sort primitive and object arrays, sort a sub-range, use a Comparator, and understand stability and complexity.

Arrays.sort() sorts an array in place into ascending order. There are overloads for every primitive array type and for object arrays. Object arrays can also take a Comparator, and any array can be sorted over just a range of indexes.

For primitives the algorithm is a dual-pivot quicksort; for objects it is TimSort, which is stable. Both run in O(n log n).

Java
int[] a = {5, 2, 8};
Arrays.sort(a);

Try it

Java Whole arrays and ranges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Comparators, descending order and 2D rows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public static void sort(int[] a)                         // also long[], short[], char[], byte[], float[], double[]
public static void sort(int[] a, int fromIndex, int toIndex)
public static void sort(Object[] a)
public static <T> void sort(T[] a, Comparator<? super T> c)
public static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
Name Description
a The array to sort in place.
fromIndex First index of the range, inclusive.
toIndex End of the range, exclusive.
c Comparator. null means natural order.

Returns: nothing; the array is changed.

Throws: NullPointerException if a is null or natural ordering meets a null element; ClassCastException if elements are not mutually comparable; IllegalArgumentException if fromIndex > toIndex; ArrayIndexOutOfBoundsException if the range leaves the array.

Usage notes

  • No comparator for primitives. To sort an int[] in descending order, sort ascending and reverse it, or box into Integer[].
  • Stability: object sorts are stable (equal elements keep their order). Primitive sorts do not need to be, since equal primitives are indistinguishable.
  • Floating point: -0.0 sorts before 0.0, and NaN goes last.
  • Strings sort by UTF-16 code units, so uppercase letters precede lowercase ones. Use String.CASE_INSENSITIVE_ORDER for a case-blind order.
  • Large arrays: Arrays.parallelSort can be faster on many cores (for arrays above a few thousand elements). It sorts to the same result.
  • Sorting a list? Use list.sort() or Collections.sort().
  • Comparator contract: a comparator that is inconsistent can make TimSort throw IllegalArgumentException: Comparison method violates its general contract!.
  • Subtraction bugs: (a, b) -> a - b overflows for extreme values. Use Integer.compare.