UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

31 lines (29 loc) 723 B
import { dfdlT } from "@monstermann/dfdl"; import { cloneArray } from "@monstermann/remmi"; //#region src/array/sort.ts /** * `sort(target, comparator)` * * Returns a new array with the elements of `target` sorted using the provided `comparator` function. * * ```ts * sort([3, 1, 4, 2], (a, b) => a - b); // [1, 2, 3, 4] * sort(["c", "a", "b"], (a, b) => a.localeCompare(b)); // ['a', 'b', 'c'] * ``` * * ```ts * pipe( * [3, 1, 4, 2], * sort((a, b) => a - b), * ); // [1, 2, 3, 4] * pipe( * ["c", "a", "b"], * sort((a, b) => a.localeCompare(b)), * ); // ['a', 'b', 'c'] * ``` */ const sort = dfdlT((target, comparator) => { return cloneArray(target).sort(comparator); }, 2); //#endregion export { sort };