import { curryN } from "@unboxing/function"; import { OrdFunc, Ord } from '@unboxing/core' interface StableSortBy { (fn: OrdFunc, list: ArrayLike): T[]; (fn: OrdFunc): (list: ArrayLike) => T[]; } /** * Sorts the array according to the supplied function and keeping the order of elements. */ export const stableSortBy = curryN(2, (fn: OrdFunc, arr: ArrayLike = []) => { const len = arr.length; const indexes = new Array(len); for (let i = 0; i < len; i++) { indexes[i] = i; } indexes.sort((a, b) => { const valueA = arr[a]; const valueB = arr[b]; const x = fn(valueA); const y = fn(valueB); if (x < y) { return -1; } else if (x > y) { return 1; } return a - b; }); const result = new Array(len); for (let i = 0; i < len; i++) { result[i] = arr[indexes[i]]; } return result; }) as StableSortBy;