@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
36 lines (34 loc) • 1.33 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDateTime } from "../validate/isValidDateTime.js";
// src/plain/calculate/sortDateTimes.ts
/**
* Sort an array of PlainDateTime values in ascending or descending order.
*
* - Returns empty array if no valid datetimes.
* - Validation is performed on each item in the array.
*
* @param dateTimes Array of ISO PlainDateTime strings (e.g. "2024-03-10T12:00:00")
* @param order "asc" for ascending (earliest first) | "desc" for descending (latest first)
* @returns Sorted array of datetime strings
*
* @example sortDateTimes(["2024-03-10T12:00:00", "2024-01-01T08:00:00", "2024-02-15T15:30:00"]) // ["2024-01-01T08:00:00", "2024-02-15T15:30:00", "2024-03-10T12:00:00"]
* @example sortDateTimes([]) // []
*/
function sortDateTimes(dateTimes, order = "asc") {
try {
if (!Array.isArray(dateTimes) || !dateTimes.length) return [];
const valid = dateTimes.filter(isValidDateTime);
if (!valid.length) return [];
try {
const comparables = valid.map((d) => Temporal.PlainDateTime.from(d));
comparables.sort(Temporal.PlainDateTime.compare);
if (order === "desc") return comparables.reverse().map((d) => d.toString());
return comparables.map((d) => d.toString());
} catch {
return [];
}
} catch {
return [];
}
}
export { sortDateTimes };