Sub topic 13
Comparing and Sorting Temporal Values
This chapter assumes you can create Temporal values with from() and constructors, and that you know the difference between exact time (Instant, ZonedDateTime) and wall-clock time (the Plain types). The examples run in Node.js with ES modules. They use the native Temporal global if the runtime has one and fall back to @js-temporal/polyfill otherwise.
Why `<` and `>` don't work
With legacy Date, a < b works because valueOf() returns epoch milliseconds. Temporal objects deliberately throw a TypeError from valueOf(). A PlainDate has no single number that represents it, and silently comparing strings or partial values would cause subtle bugs. Temporal gives you two explicit tools instead:
- The static
compare(a, b)method returns-1,0, or1, so you can pass it straight toArray.prototype.sort. - The instance method
a.equals(b)returns a boolean.
Sorting and finding extremes
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const dates = ['2024-05-01', '2023-12-25', '2024-01-15'].map((s) =>
Temporal.PlainDate.from(s)
);
dates.sort(Temporal.PlainDate.compare);
console.log(dates.map(String)); // ['2023-12-25', '2024-01-15', '2024-05-01']
// Descending order: swap the arguments
dates.sort((a, b) => Temporal.PlainDate.compare(b, a));
// Latest date without sorting
const latest = dates.reduce((a, b) =>
Temporal.PlainDate.compare(a, b) >= 0 ? a : b
);
console.log(latest.toString()); // '2024-05-01'
// compare also accepts strings and property bags
console.log(Temporal.PlainDate.compare('2024-01-01', latest)); // -1
try {
dates[0] < dates[1];
} catch (e) {
console.log(e instanceof TypeError); // true
}Every Temporal type with comparable values has its own compare. Use the one that matches your data, such as Temporal.Instant.compare or Temporal.PlainTime.compare.
`compare` returning 0 is not the same as `equals`
compare looks only at position in time. equals also checks the other fields. For a ZonedDateTime, compare compares the exact instant, while equals also requires the same time zone and calendar.
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const utc = Temporal.ZonedDateTime.from('2024-03-15T12:00[UTC]');
const tokyo = utc.withTimeZone('Asia/Tokyo');
console.log(Temporal.ZonedDateTime.compare(utc, tokyo)); // 0
console.log(utc.equals(tokyo)); // falsePlain types follow the same rule. PlainDate.compare orders dates by their ISO date and ignores the calendar, but equals returns false when the calendars differ.
Comparing durations
Temporal.Duration.compare works directly when the durations contain only days or smaller units. Days count as 24 hours in that case. Years, months, and weeks vary in length, so you must pass a relativeTo date. Without one, the method throws a RangeError.
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
console.log(Temporal.Duration.compare({ hours: 1, minutes: 30 }, { minutes: 90 })); // 0
const relativeTo = Temporal.PlainDate.from('2024-02-01');
console.log(Temporal.Duration.compare({ months: 1 }, { days: 30 }, { relativeTo })); // -1February 2024 has 29 days, so one month counted from February 1 is shorter than 30 days.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| `a < b` | `TypeError` | `Type.compare(a, b) < 0` |
| `a === b` | Compares object references | `a.equals(b)` |
| Using `equals` to test for the same instant across zones | `false` | `compare(a, b) === 0` |
| Comparing durations that contain months | `RangeError` | Pass `{ relativeTo }` |
Exercise
You have an array of ZonedDateTime meeting times in mixed time zones. How do you sort them chronologically, and why is the order correct even though their wall-clock times differ?
Show answer
Call meetings.sort(Temporal.ZonedDateTime.compare). That method compares the exact instants, not the local wall-clock fields, so a 21:00 Tokyo meeting and a 12:00 UTC meeting on the same day are treated as the same moment.
Exercise
What does Temporal.Duration.compare({ weeks: 1 }, { days: 7 }) do with no options?
Show answer
It throws a RangeError, because the first duration contains weeks, which are a calendar unit. Pass a relativeTo option, for example { relativeTo: Temporal.PlainDate.from('2024-01-01') }, and it returns 0.