Java Arrays.compare() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 9+

Definition

In Java, Arrays.compare(a, b) compares two arrays lexicographically, the way a dictionary orders words: the first index where the elements differ decides. If one array is a prefix of the other, the shorter one comes first.

The result is negative if a comes first, zero if the arrays are equal, and positive if a comes after b. Only the sign is meaningful, so test < 0 or > 0 rather than expecting -1 or 1.

A static method of java.util.Arrays (Java 9+): call it on the class, as in Arrays.compare(...); there is no Arrays object to create.

Java 9+ only.

Added in Java 9: the examples need JDK 9 or newer, and on Java 8 the call fails with "cannot find symbol". On Java 8, loop to the first differing index and compare those elements with Integer.compare; if none differ, compare the lengths.

Syntax

Pass two arrays of the same type; check the sign of the result.

Java
int[] a = {1, 2, 3};
int[] b = {1, 3};
Arrays.compare(a, b) < 0;              // true: 2 < 3 at index 1
Arrays.compare(b, a) > 0;              // true
Arrays.compare(a, new int[] {1, 2, 3}); // 0
CallWhat it does
Arrays.compare(a, b)Order of two primitive or Comparable arrays.
Arrays.compare(x, y, comparator)Object elements compared with a comparator.
Arrays.compare(a, aFrom, aTo, b, bFrom, bTo)Compares a range of each array.
Arrays.sort(rows, (x, y) -> Arrays.compare(x, y))Sort an int[][] row by row.

Overloads cover every primitive type (for boolean[], false < true), Comparable object arrays, and object arrays with a Comparator, each with a range form.

Library declarations
Java
// java.util.Arrays (since Java 9)
public static int compare(int[] a, int[] b)
public static <T extends Comparable<? super T>> int compare(T[] a, T[] b)
public static <T> int compare(T[] a, T[] b, Comparator<? super T> cmp)
// range form: compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex)
// also boolean[], byte[], short[], char[], long[], float[], double[]

Parameters

ParameterDescription
a, bThe arrays to compare. In the two-array forms, either may be null.
range indexes (optional)From (inclusive) and to (exclusive) indexes for each array.
cmp (optional)Comparator for the elements of object arrays.

Return Value

Return ValueDescription
int < 0a comes first: smaller at the first difference, or a proper prefix of b.
0Same length and equal elements.
int > 0a comes after b.

Compare with Similar Methods

Related array comparisons:

MethodHow it differs
Arrays.compareUnsigned(a, b)Treats integer elements as unsigned values.
Arrays.mismatch(a, b)Returns the index of the first difference instead of an order.
Arrays.equals(a, b)Only answers equal or not; compare returns 0 exactly when it is true.
Arrays.sort(rows, cmp)Use compare as the comparator to sort arrays of arrays.

Java Examples

Example 1 — Compare Version Numbers Stored as Arrays

Version 1.4.2 against 1.5, 1.4.2.0, and an identical copy.

Java
import java.util.Arrays;

public class ArraysCompareDemo {
    public static void main(String[] args) {
        int[] v142 = {1, 4, 2};
        int[] v15 = {1, 5};
        int[] v1420 = {1, 4, 2, 0};

        System.out.println("1.4.2 vs 1.5:     " + Integer.signum(Arrays.compare(v142, v15)));
        System.out.println("1.4.2.0 vs 1.4.2: " + Integer.signum(Arrays.compare(v1420, v142)));
        System.out.println("1.4.2 vs copy:    " + Arrays.compare(v142, new int[] {1, 4, 2}));
    }
}

How It Works

  1. First difference decides. Index 0 matches; at index 1, 4 is less than 5, so 1.4.2 comes first and the result is negative.
  2. Only the sign. Integer.signum maps any negative result to -1 and any positive one to 1, since the exact number is not specified.
  3. Prefix rule. 1.4.2.0 contains all of 1.4.2 plus one more element, so the longer array comes after: positive.
  4. Equal arrays give exactly zero. Same length and same elements always return 0, so it is safe to print directly.

Example 2 — Sort an Array of Versions

Use Arrays.compare as the comparator for an int[][], and see why text comparison gets it wrong.

Java
import java.util.Arrays;

public class ArraysCompareSortVersions {
    public static void main(String[] args) {
        int[][] versions = { {2, 0}, {1, 10, 1}, {1, 9}, {1, 10} };
        Arrays.sort(versions, (x, y) -> Arrays.compare(x, y));
        System.out.println(Arrays.deepToString(versions));

        System.out.println("as text, 1.9 > 1.10? " + ("1.9".compareTo("1.10") > 0));
    }
}

How It Works

  1. A comparator for rows. Each element of versions is an int[], and the lambda hands two rows to Arrays.compare.
  2. Numbers, not digits. 9 is less than 10, so {1, 9} sorts before {1, 10}, the correct version order.
  3. Prefix first. {1, 10} comes before {1, 10, 1}; {2, 0} is last because 2 beats 1 at index 0.
  4. Text order differs. As strings, "1.9" and "1.10" differ at '9' versus '1', so 1.9 wrongly sorts after 1.10.

Example 3 — String Arrays, null, and Ranges

Compare String[] arrays, including null elements and a null array, then compare two ranges.

Java
import java.util.Arrays;

public class ArraysCompareObjects {
    public static void main(String[] args) {
        String[] a = {"cat", "dog"};
        String[] b = {"cat", "cow"};
        String[] c = {"cat", null};
        System.out.println("a vs b: " + Integer.signum(Arrays.compare(a, b)));
        System.out.println("c vs a: " + Integer.signum(Arrays.compare(c, a)));
        System.out.println("null vs a: " + Integer.signum(Arrays.compare((String[]) null, a)));

        int[] x = {5, 6, 7, 8};
        int[] y = {0, 6, 7};
        System.out.println("x[1..3) vs y[1..3): " + Arrays.compare(x, 1, 3, y, 1, 3));
    }
}

How It Works

  1. compareTo on elements. "dog" comes after "cow" because 'd' > 'c', so a is after b.
  2. null elements first. At index 1, c has null where a has "dog", and null sorts before any string.
  3. A typed null array. The cast (String[]) null picks the overload; a null array comes before any real array.
  4. Ranges can start anywhere. Both ranges hold {6, 7}, so the result is 0 even though index 0 differs.

Frequently Asked Questions

A negative number if the first array comes first, zero if the arrays are equal, and a positive number if it comes after. Only the sign is meaningful.
Java 9. On Java 8 the method does not exist, so you need a hand-written loop.
It compares elements up to the shorter length. If they are all equal, the shorter array comes first.
A null array comes before any non-null array, and two null arrays are equal. Inside object arrays, null elements come before non-null elements.
Pass Arrays.compare as the comparator: Arrays.sort(rows, (x, y) -> Arrays.compare(x, y)). This needs Java 9 or newer.

Next: Arrays.compareUnsigned()

Compare integer arrays as unsigned numbers (Java 9).

compareUnsigned() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful