source: node_modules/d3-array/src/sort.js@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import ascending from "./ascending.js";
2import permute from "./permute.js";
3
4export default function sort(values, ...F) {
5 if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable");
6 values = Array.from(values);
7 let [f] = F;
8 if ((f && f.length !== 2) || F.length > 1) {
9 const index = Uint32Array.from(values, (d, i) => i);
10 if (F.length > 1) {
11 F = F.map(f => values.map(f));
12 index.sort((i, j) => {
13 for (const f of F) {
14 const c = ascendingDefined(f[i], f[j]);
15 if (c) return c;
16 }
17 });
18 } else {
19 f = values.map(f);
20 index.sort((i, j) => ascendingDefined(f[i], f[j]));
21 }
22 return permute(values, index);
23 }
24 return values.sort(compareDefined(f));
25}
26
27export function compareDefined(compare = ascending) {
28 if (compare === ascending) return ascendingDefined;
29 if (typeof compare !== "function") throw new TypeError("compare is not a function");
30 return (a, b) => {
31 const x = compare(a, b);
32 if (x || x === 0) return x;
33 return (compare(b, b) === 0) - (compare(a, a) === 0);
34 };
35}
36
37export function ascendingDefined(a, b) {
38 return (a == null || !(a >= a)) - (b == null || !(b >= b)) || (a < b ? -1 : a > b ? 1 : 0);
39}
Note: See TracBrowser for help on using the repository browser.