import type { Comparator , Fn } from "@thi.ng/api" ; import { isFunction } from "@thi.ng/checks/is-function" ; import { compare } from "@thi.ng/compare/compare" ; import { assert } from "@thi.ng/errors/assert" ; import { quickSort } from "./quicksort.js" ; import { multiSwap } from "./swap.js" ; /**
 * Takes a `src` array and `key` array of function to provide the sort key of
 * each item. If a function, it will be first applied to pre-compute a new array
 * of all sort keys. Then uses  quickSort to sort `src` array, based on
 * the ordering of cached keys and the optionally given comparator. Returns
 * `src`.
 *
 *  remarks example param src param key param cmp export const sortByCachedKey = < T , K > ( src , key , cmp = compare ) => { const keys = isFunction ( key ) ? src . map ( key ) : key ; assert ( keys . length === src . length , `keys.length != src.length` ) ; quickSort ( keys , cmp , multiSwap ( src ) ) ; return src ; } ;