Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use assertThat(values).isSorted() to check ascending natural order, or assertThat(values).isSortedAccordingTo(comparator) when the required order is defined by a comparator. Both methods verify the existing list; they do not sort or mutate it.

Choose the assertion that matches the required order

Assertion Ordering rule Use it when
isSorted() Ascending natural order The elements have suitable, mutually compatible Comparable semantics.
isSortedAccordingTo(comparator) The order defined by the supplied Comparator The requirement is reverse order or a domain-specific ordering.

The method contracts below are documented in the AssertJ Core 3.27.7 AbstractListAssert API.

Assert ascending natural order with isSorted()

For values whose natural order is the expected ascending order, call isSorted() on the list assertion:

import static org.assertj.core.api.Assertions.assertThat;

List<Integer> values = List.of(1, 2, 3);
assertThat(values).isSorted();

The list elements must be mutually comparable. It is not enough for each element to implement Comparable if values of different runtime types cannot compare with one another. For domain objects, use this assertion only when their natural ordering expresses the requirement under test.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check a comparator-defined order

Pass the expected ordering directly to isSortedAccordingTo. This makes the rule visible at the assertion, including when checking descending order:

import static org.assertj.core.api.Assertions.assertThat;
import static java.util.Comparator.reverseOrder;

List<Integer> values = List.of(3, 2, 1);
assertThat(values).isSortedAccordingTo(reverseOrder());

A comparator can also state a business ordering for objects:

Comparator<Person> byLastName = Comparator.comparing(Person::lastName);
assertThat(people).isSortedAccordingTo(byLastName);

Use the same comparator semantics that define the production requirement. If equal primary keys must appear in a deterministic order, add an appropriate tie-breaker to the comparator; sortedness under the primary comparator alone does not specify the order among elements it considers equal.

Understand empty, singleton, and invalid inputs

  • Empty list: Both assertions consider an empty list sorted.
  • One element: Natural-order checking requires the element type to support the documented Comparable requirement; comparator-based checking requires the element to be compatible with the supplied comparator.
  • Null list: The assertion fails with AssertionError.
  • Null comparator: Passing one to isSortedAccordingTo throws NullPointerException.
  • Incompatible elements: If values cannot be compared under the selected natural order or comparator, the sortedness check fails.
  • Unsorted list: When the documented order condition is not met, the assertion fails with AssertionError.

Do not confuse usingComparator with the sorting comparator

usingComparator(...) configures comparison behavior for incoming assertion checks. It is not the way to specify the order to test. For sortedness under an explicit ordering, pass the comparator to isSortedAccordingTo(comparator), as documented by the AssertJ 3.27.7 list assertion API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep the assertion readable in a fluent chain

Both sortedness methods return the fluent assertion object, so they can be followed by other assertions when the sequence remains clear:

assertThat(values)
    .isNotEmpty()
    .isSorted();

Choose a chain that reflects the requirement rather than adding checks that merely repeat it.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Related array assertions have a separate contract

AssertJ provides sorting assertions for arrays as well, but the array API is related rather than interchangeable with the List contract. Consult the AssertJ Core 3.27.7 ArraySortedAssert API before applying List-specific edge-case behavior to an array.

For runnable project examples, the AssertJ examples repository describes executable assertion examples; its main branch follows the latest released modules and can change over time.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.