cmp

Compares two month values chronologically and returns -1, 0, or 1 — the sort order of the receiver relative to the other month.

import { Month } from "@teakit/month";

const month = Month.from("2025-01");

month.cmp("2025-02"); // -1
month.cmp("2025-01"); // 0
month.cmp("2024-12"); // 1

Month.cmp("2025-01", "2025-02"); // -1

// Use as a sort comparator.
["2025-03", "2025-01", "2025-02"].sort((a, b) => Month.cmp(a, b));
// ["2025-01", "2025-02", "2025-03"]

API Reference

Signatures

month.cmp(other: MonthValue): 1 | -1 | 0;
Month.cmp(left: MonthValue, right: MonthValue): 1 | -1 | 0;

Parameters

ParameterTypeRequiredNotes
otherMonthValueInstance onlyMonth compared against the receiver.
leftMonthValueStatic onlyLeft-hand month.
rightMonthValueStatic onlyRight-hand month.

Returns

Returns -1 when the left month is earlier, 1 when later, and 0 when the two months are equal.

Agent Contract

FieldValue
Kindcomparison
Canonical namecmp
Mutates receiverNo
Orderingchronological
Returns1 | -1 | 0
Related methodseq, ne, lt, le, gt, ge, min, max

Agent Notes

  • Use cmp when a sort callback needs a numeric ordering value.
  • For boolean checks, use the comparison predicates eq, ne, lt, le, gt, ge instead of inspecting the cmp result.