Skip to content

fix(sort): treat null dates as oldest in sortByDate - #27

Merged
ManningWorks merged 5 commits into
mainfrom
fix/sort-by-date-null-dates
Aug 18, 2026
Merged

fix(sort): treat null dates as oldest in sortByDate#27
ManningWorks merged 5 commits into
mainfrom
fix/sort-by-date-null-dates

Conversation

@ManningWorks

@ManningWorks ManningWorks commented Aug 18, 2026

Copy link
Copy Markdown
Owner

Fixes #23

Problem

sortByDate(projects, 'desc') (the default "newest first") sorted projects with no date (updatedAt and createdAt both null) to the top, ahead of every dated project. The same dateless row also flipped position with sort direction — top in desc, bottom in asc — because the null-comparison branches multiplied by the order multiplier.

Change

packages/core/src/lib/sortByDate.ts — replaced the four null special-case branches with epoch-equivalent substitution: a missing date compares as 0 (oldest). The asc/desc multiplier now applies uniformly, so dateless projects sort last in newest-first and first in oldest-first, consistently in both directions. Net effect: 4 branches → 2 lines.

This also aligns the implementation with the already-published docs (packages/docs/src/api/utilities/sort-by-date.md), which state: "Projects with no date are placed at the end (for 'desc') or beginning (for 'asc')".

Tests

Updated the two tests that encoded the old behavior (dateless now asserted at end in desc, beginning in asc) and added:

  • Dateless at end when order is omitted (default desc)
  • Dateless sorts after createdAt-only projects in desc (guards the fallback chain)
  • Dateless sorts before createdAt-only projects in asc (mirror case)

Review follow-up: the mirrored desc/asc pairs are collapsed into it.each cases, and a redundant both-null test was dropped (999 → 998 tests, same behavioural coverage).

Changelog

Added an [Unreleased] → Fixed entry with a reference link to #23, per the public-API documentation requirement.

Out of scope

Verification

pnpm build && pnpm typecheck && pnpm lint && pnpm test — all pass (998 tests, 61 files).

Null dates previously sorted to the TOP in desc order (and the bottom in
asc), making dateless projects flip position with sort direction. Treat
null as epoch-equivalent so dateless projects sort last in newest-first
and first in oldest-first, consistently in both directions. This also
matches the already-published docs for sortByDate.

Fixes #23
@vercel

vercel Bot commented Aug 18, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
projex-docs Ready Ready Preview Aug 18, 2026 9:49pm

Address PR #27 review: clarify epoch-equivalent comment, collapse the
mirrored desc/asc tests with it.each, and drop the redundant both-null
test.
Type it.each rows via generic instead of repeated as-casts, assert
stable input order in the all-dateless test, and restore ascending
issue-number order of CHANGELOG link refs.
@greptile-apps

greptile-apps Bot commented Aug 18, 2026

Copy link
Copy Markdown

Greptile Summary

The PR updates sortByDate so missing dates compare as older than every valid date.

  • Uses negative infinity for missing timestamps, including against pre-1970 dates.
  • Adds parameterized coverage for ascending, descending, default-order, fallback-date, and pre-1970 cases.
  • Adds an Unreleased changelog entry for the null-date correction.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
packages/core/src/lib/sortByDate.ts Replaces direction-sensitive null branches with negative-infinity timestamps, correctly preserving null placement against all valid dates.
packages/core/src/lib/tests/sortByDate.test.ts Adds focused regression coverage for both sort directions, omitted order, createdAt fallback, stable dateless ordering, and pre-1970 dates.
CHANGELOG.md Adds the Unreleased public-API fix entry and issue reference.

Reviews (2): Last reviewed commit: "fix(sort): keep dateless projects oldest..." | Re-trigger Greptile

Comment thread packages/core/src/lib/sortByDate.ts Outdated
The epoch-equivalent (0) sentinel sorted any real pre-1970 date as
older than dateless projects, breaking the documented end/beginning
placement. Use -Infinity so a missing date is older than any real
date. NaN from (-Inf) - (-Inf) is treated as 0 by the sort spec,
keeping the all-dateless case stable.
@ManningWorks
ManningWorks merged commit e017d8e into main Aug 18, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sortByDate sorts null/missing dates to the TOP in descending order

1 participant