Skip to content
Buy me a coffee

Comparator

Comparator: object

Defined in: src/util/Comparator.ts:5

Comparator factories and combinators. Comparators are plain functions, so the reference’s default methods become functions taking the comparator first: c.reversed() → reversed(c), c.thenComparing(other) → thenComparing(c, other), c.thenComparing(key[, keyCmp]) → thenComparingBy(c, key[, keyCmp]), c.thenComparingInt/Long/Double(key) → thenComparingInt/Long/Double(c, key).

comparing<T, U>(key, keyComparator?): Comparator<T>

Compares by key; keyComparator defaults to natural ordering.

T

U

(t) => U

Comparator<U>

Comparator<T>

NullPointerException for a null key or an explicitly null keyComparator

comparingDouble<T>(key): Comparator<T>

Compares by the numeric key that key extracts (NaN last, -0 equal to 0).

T

(t) => number

Comparator<T>

NullPointerException if key is null

comparingInt<T>(key): Comparator<T>

Compares by the numeric key that key extracts (NaN last, -0 equal to 0).

T

(t) => number

Comparator<T>

NullPointerException if key is null

comparingLong<T>(key): Comparator<T>

Compares by the number or bigint key that key extracts.

T

(t) => number | bigint

Comparator<T>

NullPointerException if key is null

max<T>(c, a, b): T

The greater of a and b by c; a when they compare equal.

T

Comparator<T>

T

T

T

min<T>(c, a, b): T

The lesser of a and b by c; a when they compare equal.

T

Comparator<T>

T

T

T

naturalOrder<T>(): Comparator<T>

Returns the natural-ordering comparator: compareTo() when the first value defines it, otherwise numbers (NaN last), strings (UTF-16 code unit order), bigints, booleans (false first) and Dates. The comparator throws NullPointerException for null/undefined and ClassCastException for values that cannot be compared with each other.

T

Comparator<T>

nullsFirst<T>(c?): Comparator<T | null | undefined>

Nulls (and undefined) before everything else. Omitted c = natural ordering; an explicit null c makes all non-null values equal.

T

Comparator<T> | null

Comparator<T | null | undefined>

nullsLast<T>(c?): Comparator<T | null | undefined>

Like nullsFirst, with nulls last.

T

Comparator<T> | null

Comparator<T | null | undefined>

reversed<T>(c): Comparator<T>

reversed(reversed(c)) === c.

T

Comparator<T>

Comparator<T>

NullPointerException if c is null

reverseOrder<T>(): Comparator<T>

Same instance as Collections.reverseOrder().

T

Comparator<T>

thenComparing<T>(first, …rest): Comparator<T>

first, then each of rest to break ties.

T

Comparator<T>

…Comparator<T>[]

Comparator<T>

NullPointerException if any comparator is null

thenComparingBy<T, U>(c, key, keyComparator?): Comparator<T>

c.thenComparing(key[, keyComparator]).

T

U

Comparator<T>

(t) => U

Comparator<U>

Comparator<T>

NullPointerException for a null argument

thenComparingDouble<T>(c, key): Comparator<T>

Returns c, with ties broken by comparingDouble(key).

T

Comparator<T>

(t) => number

Comparator<T>

NullPointerException if c or key is null

thenComparingInt<T>(c, key): Comparator<T>

Returns c, with ties broken by comparingInt(key).

T

Comparator<T>

(t) => number

Comparator<T>

NullPointerException if c or key is null

thenComparingLong<T>(c, key): Comparator<T>

Returns c, with ties broken by comparingLong(key).

T

Comparator<T>

(t) => number | bigint

Comparator<T>

NullPointerException if c or key is null