Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* handle undefined during bisection * avoid duplicate lo < hi test * optimize compare1 * handle undefined during sort * descending sort test * better error for null accessor * adopt default arguments
- Loading branch information
Showing
6 changed files
with
90 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
export default function ascending(a, b) { | ||
return a == null || b == null ? NaN | ||
: a < b ? -1 | ||
: a > b ? 1 | ||
: a >= b ? 0 | ||
: NaN; | ||
return a == null || b == null ? NaN : a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN; | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,37 @@ | ||
import ascending from "./ascending.js"; | ||
import permute from "./permute.js"; | ||
|
||
export default function sort(values, ...F) { | ||
if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable"); | ||
values = Array.from(values); | ||
let [f = ascending] = F; | ||
if (f.length === 1 || F.length > 1) { | ||
let [f] = F; | ||
if ((f && 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]); | ||
const c = ascendingDefined(f[i], f[j]); | ||
if (c) return c; | ||
} | ||
}); | ||
} else { | ||
f = values.map(f); | ||
index.sort((i, j) => ascending(f[i], f[j])); | ||
index.sort((i, j) => ascendingDefined(f[i], f[j])); | ||
} | ||
return permute(values, index); | ||
} | ||
return values.sort(f); | ||
return values.sort(f === undefined ? ascendingDefined : compareDefined(f)); | ||
} | ||
|
||
export function compareDefined(compare) { | ||
if (typeof compare !== "function") throw new TypeError("compare is not a function"); | ||
return (a, b) => { | ||
const x = compare(a, b); | ||
if (x || x === 0) return x; | ||
return (compare(b, b) === 0) - (compare(a, a) === 0); | ||
}; | ||
} | ||
|
||
export function ascendingDefined(a, b) { | ||
return (a == null || !(a >= a)) - (b == null || !(b >= b)) || (a < b ? -1 : a > b ? 1 : 0); | ||
} |
This file contains 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
This file contains 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