Skip to content

sortByDate: invalid date strings produce NaN comparator results (undefined sort order) #29

Description

@ManningWorks

Problem

sortByDate resolves each project's date as updatedAt || createdAt || null, then converts via new Date(date).getTime(). A truthy-but-unparseable string (e.g. "not-a-date") yields NaN, so the comparator returns NaN and the resulting order is unspecified per the ECMAScript sort contract.

// packages/core/src/lib/sortByDate.ts
const timeA = dateA ? new Date(dateA).getTime() : 0  // 'not-a-date' → NaN
return (timeA - timeB) * multiplier                  // NaN

Context

Proposal

Coerce NaN to 0 in the comparator so unparseable dates degrade to dateless (epoch-equivalent, oldest) rather than poisoning the sort:

const timeA = dateA ? new Date(dateA).getTime() || 0 : 0

(|| 0 also swallows a literal epoch 0, which is acceptable — it already ties with dateless per #23.)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions