Conversation
sortPayments compared String(amount).localeCompare(String(amount)) instead of the numbers themselves, so a shorter amount with a higher leading digit (e.g. 994) ranked above a longer, larger amount (e.g. 9873) — visible on /payments and GET /api/payments with sort=amount. Every other localeCompare in the codebase sorts ISO date strings, which is safe (fixed-width, lexicographic order matches chronological order); this was the only place applying string comparison to a number. analytics.ts already sorts numerically elsewhere (b.volume - a.volume) — this fix matches that existing pattern instead of introducing a new one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
Sorting payments by amount was comparing amounts as strings instead of numbers, so a shorter amount with a high leading digit (like 994) could rank above a larger amount (like 9873) on both
/paymentsandGET /api/payments?sort=amount. Amounts now sort by actual numeric value.How I verified it
npx vitest run src/data/queries.test.tsagainst the old comparator — failed as expected:After the fix, same test passes, and
npm test— 4 files, 29 tests, all passing (money/date/csv unaffected,queries.test.tsis new).Checked live against the running dev server:
curl "http://localhost:3000/api/payments?sort=amount&direction=desc"— top 5 are now47980, 47935, 47880, 47826, 47778(strictly decreasing; previously three- and four-digit amounts were interleaved out of order).Acceptance criteria
(No ticket for this — these are the asks from the bug report itself.)
Deliberately not done
docs/tickets/for this — it was found ad hoc while manually testing the export feature, not from a tracked backlog item.localeComparebeing misapplied to a non-string field elsewhere in the future — this fix is scoped to the one confirmed instance.GET /api/payments/export) calls the samesortPayments, so it's fixed automatically, not as a separate change — worth knowing it's a shared fix surface, not something addressed twice.