-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8284ce
commit 4b65c36
Showing
2 changed files
with
34 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { compare } from "@thi.ng/compare/compare"; | ||
import { fillRange } from "./fill-range.js"; | ||
import { sortByCachedKey } from "./sort-cached.js"; | ||
|
||
/** | ||
* Returns a new array of numeric indices in same order as sorted values of | ||
* `src` using given (optional) comparator (default: thi.ng/compare). The `src` | ||
* array will remain unmodified. | ||
* | ||
* @remarks | ||
* Also see {@link swizzle} to read an array in custom order. | ||
* | ||
* @example | ||
* ```ts | ||
* const src = ["a", "c", "d", "b"]; | ||
* | ||
* argSort(src) | ||
* // [ 0, 3, 1, 2 ] | ||
* | ||
* // src[0] => "a" | ||
* // src[3] => "b" | ||
* // src[1] => "c" | ||
* // src[2] => "d" | ||
* | ||
* swizzle(argSort(src))(src) | ||
* // [ 'a', 'b', 'c', 'd' ] | ||
* ``` | ||
* | ||
* @param src - | ||
* @param cmp - | ||
*/ | ||
export const argSort = <T>(src: T[], cmp = compare) => | ||
sortByCachedKey(fillRange(new Array(src.length)), src.slice(), cmp); |
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