UNPKG

everyutil

Version:

A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.

22 lines (21 loc) 696 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.arrayStableSort = void 0; /** * Stable sort for arrays (guarantees order of equal elements). * @author @dailker * @template T * @param {T[]} array - The array to sort. * @param {(a: T, b: T) => number} compareFn - Comparison function. * @returns {T[]} Sorted array. */ function arrayStableSort(array, compareFn) { return array .map((item, idx) => ({ item, idx })) .sort((a, b) => { const cmp = compareFn(a.item, b.item); return cmp !== 0 ? cmp : a.idx - b.idx; }) .map(({ item }) => item); } exports.arrayStableSort = arrayStableSort;