Skip to content

Commit

Permalink
null-safe sort (observablehq#1348)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock authored Mar 16, 2023
1 parent 9cafef3 commit d4a5f0b
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/channel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ascending, descending, InternSet, rollup, sort} from "d3";
import {InternSet, rollup, sort} from "d3";
import {ascendingDefined, descendingDefined} from "./defined.js";
import {first, isColor, isEvery, isIterable, isOpacity, labelof, map, maybeValue, range, valueof} from "./options.js";
import {registry} from "./scales/index.js";
import {isSymbol, maybeSymbol} from "./symbol.js";
Expand Down Expand Up @@ -153,9 +154,9 @@ function values(channels, name, alias) {
}

function ascendingGroup([ak, av], [bk, bv]) {
return ascending(av, bv) || ascending(ak, bk);
return ascendingDefined(av, bv) || ascendingDefined(ak, bk);
}

function descendingGroup([ak, av], [bk, bv]) {
return descending(av, bv) || ascending(ak, bk);
return descendingDefined(av, bv) || ascendingDefined(ak, bk);
}
173 changes: 173 additions & 0 deletions test/output/athletesSortWeightLimit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions test/plots/athletes-sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ export async function athletesSortNullLimit() {
marks: [Plot.dot(athletes, {x: "height", y: "weight", stroke: "nationality", sort: {color: null, limit: 10}})]
});
}

export async function athletesSortWeightLimit() {
const athletes = await d3.csv<any>("data/athletes.csv", d3.autoType);
return Plot.plot({
color: {legend: true},
marks: [
Plot.dot(athletes, {x: "weight", y: "nationality", sort: {y: "x", reduce: "median", limit: 10}}),
Plot.tickX(athletes, Plot.groupY({x: "median"}, {x: "weight", y: "nationality", stroke: "red", strokeWidth: 2}))
]
});
}

0 comments on commit d4a5f0b

Please sign in to comment.