Skip to content

fix: correct date field sorting in collections#109

Merged
liamwalder merged 2 commits intodevelopfrom
fix/collection-date-sorting
Mar 30, 2026
Merged

fix: correct date field sorting in collections#109
liamwalder merged 2 commits intodevelopfrom
fix/collection-date-sorting

Conversation

@lunenas
Copy link
Copy Markdown
Collaborator

@lunenas lunenas commented Mar 27, 2026

Summary

Collection items sorted by a date field were not ordering chronologically. Dates within the same year appeared in arbitrary order because parseFloat("2025-04-01") returns 2025, making all dates in the same year compare as equal.

Changes

  • Replace parseFloat() with Number() for numeric detection in all three collection sorting paths:
    • lib/page-fetcher.ts — SSR collection rendering
    • app/ycode/api/collections/[id]/items/route.ts — items API endpoint
    • app/ycode/api/collections/[id]/items/filter/route.ts — filterable collection endpoint
  • Number("2025-04-01") correctly returns NaN, so date strings fall through to localeCompare where ISO format sorts chronologically
  • Add empty-string guard to prevent Number("") (which is 0) from triggering false numeric comparison

Test plan

  • Sort a collection by a date field in ascending order — verify chronological order
  • Sort a collection by a date field in descending order — verify reverse chronological order
  • Sort by a numeric field — verify numeric sorting still works
  • Sort by a text field — verify alphabetical sorting still works
  • Verify sorting works in the published website (SSR path)
  • Verify sorting works in the editor canvas (API path)
  • Verify sorting works with filterable collections (filter API path)

Made with Cursor

Replace parseFloat with Number for numeric detection in collection
item sorting. parseFloat("2025-04-01") returns 2025, causing all
dates in the same year to compare as equal. Number() correctly
returns NaN for date strings, allowing them to fall through to
string comparison where ISO format sorts chronologically.

Made-with: Cursor
@liamwalder liamwalder self-assigned this Mar 30, 2026
@liamwalder liamwalder merged commit a26b65a into develop Mar 30, 2026
2 checks passed
@liamwalder liamwalder deleted the fix/collection-date-sorting branch March 30, 2026 12:47
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.

2 participants