Skip to content

Commit

Permalink
optimize for single accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jan 24, 2021
1 parent becb502 commit f819290
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/sort.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import ascending from "./ascending.js";
import permute from "./permute.js";

export default function sort(values, ...order) {
export default function sort(values, ...F) {
if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable");
values = Array.from(values);
const [f = ascending] = order;
if (f.length === 1 || order.length > 1) {
order = order.map(f => values.map(f));
return permute(values, values.map((d, i) => i).sort((i, j) => {
for (const f of order) {
const c = ascending(f[i], f[j]);
if (c) return c;
}
}));
let [f = ascending] = F;
if (f.length === 1 || F.length > 1) {
const index = Uint32Array.from(values, (d, i) => i);
if (F.length > 1) {
F = F.map(f => values.map(f));
index.sort((i, j) => {
for (const f of F) {
const c = ascending(f[i], f[j]);
if (c) return c;
}
});
} else {
f = values.map(f);
index.sort((i, j) => ascending(f[i], f[j]));
}
return permute(values, index);
}
return values.sort(f);
}

0 comments on commit f819290

Please sign in to comment.