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).
Type Declaration
Section titled “Type Declaration”comparing()
Section titled “comparing()”comparing<
T,U>(key,keyComparator?):Comparator<T>
Compares by key; keyComparator defaults to natural ordering.
Type Parameters
Section titled “Type Parameters”T
U
Parameters
Section titled “Parameters”(t) => U
keyComparator?
Section titled “keyComparator?”Comparator<U>
Returns
Section titled “Returns”Comparator<T>
Throws
Section titled “Throws”NullPointerException for a null key or an explicitly null keyComparator
comparingDouble()
Section titled “comparingDouble()”comparingDouble<
T>(key):Comparator<T>
Compares by the numeric key that key extracts (NaN last, -0 equal to 0).
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”(t) => number
Returns
Section titled “Returns”Comparator<T>
Throws
Section titled “Throws”NullPointerException if key is null
comparingInt()
Section titled “comparingInt()”comparingInt<
T>(key):Comparator<T>
Compares by the numeric key that key extracts (NaN last, -0 equal to 0).
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”(t) => number
Returns
Section titled “Returns”Comparator<T>
Throws
Section titled “Throws”NullPointerException if key is null
comparingLong()
Section titled “comparingLong()”comparingLong<
T>(key):Comparator<T>
Compares by the number or bigint key that key extracts.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”(t) => number | bigint
Returns
Section titled “Returns”Comparator<T>
Throws
Section titled “Throws”NullPointerException if key is null
max<
T>(c,a,b):T
The greater of a and b by c; a when they compare equal.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Comparator<T>
T
T
Returns
Section titled “Returns”T
min<
T>(c,a,b):T
The lesser of a and b by c; a when they compare equal.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Comparator<T>
T
T
Returns
Section titled “Returns”T
naturalOrder()
Section titled “naturalOrder()”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.
Type Parameters
Section titled “Type Parameters”T
Returns
Section titled “Returns”Comparator<T>
nullsFirst()
Section titled “nullsFirst()”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.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Comparator<T> | null
Returns
Section titled “Returns”Comparator<T | null | undefined>
nullsLast()
Section titled “nullsLast()”nullsLast<
T>(c?):Comparator<T|null|undefined>
Like nullsFirst, with nulls last.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Comparator<T> | null
Returns
Section titled “Returns”Comparator<T | null | undefined>
reversed()
Section titled “reversed()”reversed<
T>(c):Comparator<T>
reversed(reversed(c)) === c.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Comparator<T>
Returns
Section titled “Returns”Comparator<T>
Throws
Section titled “Throws”NullPointerException if c is null
reverseOrder()
Section titled “reverseOrder()”reverseOrder<
T>():Comparator<T>
Same instance as Collections.reverseOrder().
Type Parameters
Section titled “Type Parameters”T
Returns
Section titled “Returns”Comparator<T>
thenComparing()
Section titled “thenComparing()”thenComparing<
T>(first, …rest):Comparator<T>
first, then each of rest to break ties.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Comparator<T>
…Comparator<T>[]
Returns
Section titled “Returns”Comparator<T>
Throws
Section titled “Throws”NullPointerException if any comparator is null
thenComparingBy()
Section titled “thenComparingBy()”thenComparingBy<
T,U>(c,key,keyComparator?):Comparator<T>
c.thenComparing(key[, keyComparator]).
Type Parameters
Section titled “Type Parameters”T
U
Parameters
Section titled “Parameters”Comparator<T>
(t) => U
keyComparator?
Section titled “keyComparator?”Comparator<U>
Returns
Section titled “Returns”Comparator<T>
Throws
Section titled “Throws”NullPointerException for a null argument
thenComparingDouble()
Section titled “thenComparingDouble()”thenComparingDouble<
T>(c,key):Comparator<T>
Returns c, with ties broken by comparingDouble(key).
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Comparator<T>
(t) => number
Returns
Section titled “Returns”Comparator<T>
Throws
Section titled “Throws”NullPointerException if c or key is null
thenComparingInt()
Section titled “thenComparingInt()”thenComparingInt<
T>(c,key):Comparator<T>
Returns c, with ties broken by comparingInt(key).
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Comparator<T>
(t) => number
Returns
Section titled “Returns”Comparator<T>
Throws
Section titled “Throws”NullPointerException if c or key is null
thenComparingLong()
Section titled “thenComparingLong()”thenComparingLong<
T>(c,key):Comparator<T>
Returns c, with ties broken by comparingLong(key).
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Comparator<T>
(t) => number | bigint
Returns
Section titled “Returns”Comparator<T>
Throws
Section titled “Throws”NullPointerException if c or key is null