Orders two values of the same kind for Array.prototype.sort(), returning negative, zero, or positive numbers. Works across moments, calendar dates, clock times, durations, and range instances.
When the first argument is a typed point or Duration, a raw second argument such as an ISO string is read as the same kind. Comparing two different kinds throws a TypeError.
Second value of the same kind, or a raw input matching a.
Returns
-1 | 0 | 1
Returns -1 when the first value comes earlier, 0 when equal, and 1 when later.
Ranges of the same kind sort by their start point first and break ties by their end point:
import { compare } from 'native-dates';import { dateRange } from '@native-dates/plugins/ranges';const windows = [ dateRange('2026-11-10', '2026-11-20'), dateRange('2026-11-10', '2026-11-14'), dateRange('2026-11-01', '2026-11-05'),];windows.sort(compare).map(String);// ['2026-11-01/2026-11-05', '2026-11-10/2026-11-14', '2026-11-10/2026-11-20']
Comparing moments across time zones
compare() orders DateTime values by their exact instant rather than their local clock digits. A 9:00 AM meeting in New York sorts before an 8:00 AM meeting on the same day in Los Angeles.
Picking boundary points
Find the minimum or maximum point across a set of dates, moments, or clock times. Both helpers accept rest arguments or a single non-empty array.