Skip to content

Fix board date sorting on downloads page#1761

Merged
makermelissa merged 2 commits intoadafruit:mainfrom
makermelissa-piclaw:fix-date-sorting
Apr 21, 2026
Merged

Fix board date sorting on downloads page#1761
makermelissa merged 2 commits intoadafruit:mainfrom
makermelissa-piclaw:fix-date-sorting

Conversation

@makermelissa-piclaw
Copy link
Copy Markdown
Contributor

Problem

Sorting boards on the downloads page by date (newest/oldest first) produces unexpected results, as reported in #1760.

Root Cause

The sort comparator in downloads.js had several issues:

  1. Tag match overrides all sort modes: The exact tag match promotion (return -2) ran before the sort-type switch statement for all sort modes, so when a search term was active, matching boards were always pushed to the top — even when the user explicitly selected date or alphabetical sorting.

  2. Asymmetric tag comparison: Only item a was checked for tag match; item b was never tested. This made the comparator inconsistent (violating the sort contract where compare(a,b) and compare(b,a) should be symmetric).

  3. Missing equality case in comparators: The date and download-count comparators used ternary returns (< ? -1 : 1) instead of subtraction, so they never returned 0 for equal values. This produced unstable sort results for boards sharing the same date or download count.

  4. filterResults also ignores sort selection: The filterResults function prepends tag-matched boards to the DOM regardless of the currently selected sort, undoing an explicit date or alphabetical sort whenever filtering re-runs.

Fix

  • Tag match promotion now only applies when the sort type is downloads (the default)
  • Both a and b are checked for tag matches symmetrically
  • Date and download comparators use arithmetic subtraction for proper three-way comparison
  • filterResults checks the active sort radio before doing tag-match reordering

Testing

  • Sort by "Date Added (Newest First)" → boards appear in correct descending date order
  • Sort by "Date Added (Oldest First)" → boards appear in correct ascending date order
  • Sort by alphabetical → unaffected by search tag matches
  • Default downloads sort with search term → tag matches still promoted to top (existing behavior preserved)

The sort comparator had several issues causing date sorting to
produce unexpected results:

1. Exact tag match promotion ran before the sort type switch for
   all sort modes, overriding date and alphabetical sorts. Tag
   match promotion now only applies to the default (downloads)
   sort.

2. The tag match only checked item 'a' and returned -2, not
   comparing 'b'. Both items are now compared symmetrically.

3. Date and download count comparators used ternary conditional
   returns instead of subtraction, never returning 0 for equal
   values. Changed to use arithmetic subtraction for proper
   three-way comparison.

4. The filterResults tag-match DOM reordering also ignored the
   current sort selection. Now respects the active sort choice.

Fixes adafruit#1760
Boards without a valid date_added value now sort to the end of
the list for both date-asc and date-desc sort modes, instead of
appearing in unpredictable positions due to NaN comparisons.
@makermelissa
Copy link
Copy Markdown
Collaborator

Tested locally and it is behaving as expected. Actually, a bit better because new boards are now at the end of the list.

@makermelissa makermelissa merged commit 4915fbd into adafruit:main Apr 21, 2026
2 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.

2 participants