chronia

compare

Overview

The compare function compares two Date objects or timestamps chronologically with configurable sort order. It provides a flexible comparison utility suitable for sorting operations and custom ordering logic in the Chronia library.

Signature

function compare(
  date1: DateInput,
  date2: DateInput,
  options?: CompareOptions,
): number;

Parameters

Parameter Type Description
date1 DateInput The first Date object, timestamp, or ISO 8601 string to compare
date2 DateInput The second Date object, timestamp, or ISO 8601 string to compare
options CompareOptions Optional comparison options. Defaults to { order: "ASC" }

CompareOptions

Property Type Description
order "ASC" \| "DESC" Sort order for comparison results. Defaults to "ASC" (ascending)

Return Value

Type Description
number Returns -1 if date1 is before date2, 1 if date1 is after date2, or 0 if they are equal. The result is inverted when order is set to "DESC". Returns NaN if either input is invalid

Description

The compare function performs chronological comparison of two dates or timestamps, making it ideal for sorting operations and custom date ordering. It accepts both Date objects and numeric timestamps, automatically handling type conversion and validation.

Specification

Returns -1 when:

Returns 1 when:

Returns 0 when:

Returns NaN when:

Behavior Notes

Use Cases

Usage Examples

Array Sorting (Ascending)

import { compare } from "chronia";

const dates = [
  new Date("2024-03-15"),
  new Date("2024-01-10"),
  new Date("2024-02-20"),
];

// Sort in ascending order (oldest to newest)
dates.sort(compare);
// Results in: [2024-01-10, 2024-02-20, 2024-03-15]

// Or explicitly specify ascending order
dates.sort((a, b) => compare(a, b, { order: "ASC" }));
// Results in: [2024-01-10, 2024-02-20, 2024-03-15]

Array Sorting (Descending)

import { compare } from "chronia";

const timestamps = [
  new Date("2024-01-01").getTime(),
  new Date("2024-12-31").getTime(),
  new Date("2024-06-15").getTime(),
];

// Sort in descending order (newest to oldest)
timestamps.sort((a, b) => compare(a, b, { order: "DESC" }));
// Results in: [2024-12-31, 2024-06-15, 2024-01-01]

Basic Comparison

import { compare } from "chronia";

// Compare Date objects (ascending order by default)
compare(new Date("2024-01-01"), new Date("2024-01-02")); // Returns: -1
compare(new Date("2024-01-02"), new Date("2024-01-01")); // Returns: 1
compare(new Date("2024-01-01"), new Date("2024-01-01")); // Returns: 0

// Compare with descending order
compare(new Date("2024-01-01"), new Date("2024-01-02"), { order: "DESC" }); // Returns: 1
compare(new Date("2024-01-02"), new Date("2024-01-01"), { order: "DESC" }); // Returns: -1

Mixed Type Comparison

import { compare } from "chronia";

const date = new Date("2024-01-01");
const timestamp = new Date("2024-01-02").getTime();

// Compare Date object with timestamp
compare(date, timestamp); // Returns: -1

// Compare timestamp with Date object
compare(timestamp, date); // Returns: 1

Date Prioritization

import { compare } from "chronia";

function getEarlierDeadline(deadline1: Date, deadline2: Date): Date {
  const result = compare(deadline1, deadline2);
  return result <= 0 ? deadline1 : deadline2;
}

function getLatestUpdate(update1: Date, update2: Date): Date {
  const result = compare(update1, update2, { order: "DESC" });
  return result <= 0 ? update1 : update2;
}

// Usage
const earlier = getEarlierDeadline(
  new Date("2024-03-15"),
  new Date("2024-03-20"),
); // Returns: 2024-03-15

const latest = getLatestUpdate(new Date("2024-01-10"), new Date("2024-01-15")); // Returns: 2024-01-15

Invalid Input Handling

import { compare } from "chronia";

// Invalid Date objects return NaN
compare(new Date("invalid"), new Date("2024-01-01")); // Returns: NaN
compare(new Date("2024-01-01"), new Date("invalid")); // Returns: NaN

// NaN inputs return NaN
compare(NaN, new Date("2024-01-01")); // Returns: NaN

// Graceful handling in sorting
const mixedDates = [
  new Date("2024-01-01"),
  new Date("invalid"),
  new Date("2024-03-01"),
];

// Filter invalid dates before sorting
const validDates = mixedDates.filter((d) => !isNaN(compare(d, d)));
validDates.sort(compare); // Sorts only valid dates