export type Order = "asc" | "desc";
export type SortByOptions = { order: Order;};
export function sortBy<T>( array: readonly T[], selector: (el: T) => number, options?: SortByOptions,): T[];export function sortBy<T>( array: readonly T[], selector: (el: T) => string, options?: SortByOptions,): T[];export function sortBy<T>( array: readonly T[], selector: (el: T) => bigint, options?: SortByOptions,): T[];export function sortBy<T>( array: readonly T[], selector: (el: T) => Date, options?: SortByOptions,): T[];export function sortBy<T>( array: readonly T[], selector: | ((el: T) => number) | ((el: T) => string) | ((el: T) => bigint) | ((el: T) => Date), options?: SortByOptions,): T[] { const len = array.length; const indexes = new Array<number>(len); const selectors = new Array<ReturnType<typeof selector> | null>(len); const order = options?.order ?? "asc";
for (let i = 0; i < len; i++) { indexes[i] = i; const s = selector(array[i]); selectors[i] = Number.isNaN(s) ? null : s; }
indexes.sort((ai, bi) => { let a = selectors[ai]; let b = selectors[bi]; if (order === "desc") { [a, b] = [b, a]; } if (a === null) return 1; if (b === null) return -1; return a > b ? 1 : a < b ? -1 : 0; });
for (let i = 0; i < len; i++) { (indexes as unknown as T[])[i] = array[indexes[i]]; }
return indexes as unknown as T[];}