Conversation
ascending returns NaN when either side is null or NaN, and NaN is falsy, so `ascending(av, bv) || ascending(ak, bk)` fell through to the key comparison instead of ranking the non-orderable value. A group whose reduced value is null sorted by key among the real values rather than last: [z:2, a:null, m:1] gave [a, m, z] rather than [m, z, a]. ascendingDefined returns 1 or -1 there, so the || no longer swallows it. The key comparison is switched too, as the issue asks. That one is not a behaviour change: sort wraps any comparator in compareDefined, whose fallback already ranks a NaN result last, so keys were ordered correctly through the wrapper. It is switched so the comparator is right on its own rather than by rescue. Fixes d3#273
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.
Fixes #273.
ascendingreturnsNaNwhen either side is null or NaN, andNaNis falsy, so the comparator insrc/groupSort.jsfell through to the key comparison whenever a reduced value was non-orderable, instead of ranking
that group. So a group whose reduced value is null sorted by key among the real values rather than
last:
ascendingDefinedreturns 1 or -1 rather thanNaNthere, so the||no longer swallows theresult.
The key comparison is switched too, and that half is not a behaviour change. The issue asks for
both, so both are here, but it is worth being exact about which one fixes something.
sortpassesany length-2 comparator through
compareDefined, whose fallbackalready ranks a
NaNresult last, so keys were ordered correctly through the wrapper. I checkednull, undefined and NaN keys against numeric and string keys, and the output is identical before and
after. Switching it makes the comparator correct on its own rather than by rescue, which is what
makes the two halves read the same way.
One test, on the half that changes. I dropped a second test I had written for the key path: it
passed against
mainas well, so it documented existing behaviour while looking like coverage ofthis change.
mocha 'test/**/*-test.js'is 530 passing, 1 pending, andeslint src testis clean, both on Node20.20.2 (mocha 8's CLI does not start on Node 26).