Skip to content

fix broken sort algorithm - #573

Open
btfcookies wants to merge 4 commits into
qbreader:mainfrom
btfcookies:feat/fix-broken-sort-algorithm
Open

fix broken sort algorithm#573
btfcookies wants to merge 4 commits into
qbreader:mainfrom
btfcookies:feat/fix-broken-sort-algorithm

Conversation

@btfcookies

@btfcookies btfcookies commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

This pr is just an extension of #563 , i made a new one since there was a bunch of version control issues on 563. This fixes the sortTable algorithm at \client\scripts\utilities\tables.js.

Fixes

  • parseFloat returns NaN for any "-" placeholders that were added by \client\db\set-list\index.js.
  • Numeric columns used to sort ascending on the first click, while numbers were in descending order on the first click. They both now sort ascending first.
  • The directional toggle used to only work on columns that were already sorted. Since direction was inferred by re-reading the current DOM order, clicking a fresh column could not produce descending. The active column and direction are now recorded on the sorted element as data-sort-column / data-sort-ascending.

Testing

  • npm run lint & npm run build run clean
  • Set list sorting was all confirmed manually in Chrome

Note- /api/set-list?expand=true returns 50 sets instead of 711, which i believe is a bug introduced by #566 , I can investigate this further in a different pr if you would like.

btfcookies and others added 4 commits August 31, 2026 21:17
The bubble sort restarted its scan from the top after every single swap and
moved one row per pass, so reversing n rows cost O(n^2) DOM mutations. On the
set list (711 rows) that locked up the tab; measured under jsdom, reversing an
ordered table took 1.5 s at 100 rows, 30 s at 250, and over 90 s at 500.

Read the rows once, sort them with a comparator, and reinsert them through a
DocumentFragment so each row moves exactly once. The same table now reverses in
16-83 ms in the browser.

Three correctness fixes come with it:

- Cells that cannot be compared (blank text, or non-numeric text in a numeric
  column) parsed to NaN. Every NaN comparison is false, so those rows never
  swapped and no row could bubble past them, leaving the table sorted in
  segments rather than as a whole. They now sort to the bottom in both
  directions.
- Numeric columns sorted descending on the first click while text columns
  sorted ascending. Every column now starts ascending.
- Direction was inferred by running an ascending pass and checking whether it
  moved anything, so descending was only reachable from an already-sorted
  column. The active column and direction are now recorded on the sorted
  element as data-sort-column and data-sort-ascending, which callers can also
  read to render header indicators.

Text now compares with Intl.Collator({ numeric: true }) so "Set 9" sorts before
"Set 10".

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016EPFeG9eLBDYimTx7ELc8b
The three count columns are filled with "-" placeholders by the first request
and only get real numbers from the second, much slower one. Clicking them
before that request lands sorted a column of placeholders, which did nothing at
all - no reorder, no feedback.

Those headers now start greyed out and unclickable, and become sortable when
the counts arrive. If that request fails they stay disabled rather than
offering a sort over data that never came; previously the failure was swallowed
entirely.

Also:

- Scope the header lookup to this table. document.querySelectorAll('th')
  matched every header cell on the page, so a table added to the shared nav
  would have shifted every column index and silently sorted the wrong column.
- Show which column is sorted and in which direction, via a caret and aria-sort
  on the header. Direction is read back from the dataset that sortTable now
  writes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016EPFeG9eLBDYimTx7ELc8b
@btfcookies

Copy link
Copy Markdown
Contributor Author

#563 can be closed if this is merged

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.

1 participant