UNPKG

diginext-utils

Version:
31 lines 816 B
/** * Sorts an array of objects by a numeric property in ascending order. * Creates a new sorted array without modifying the original. * * @template T - The type of elements in the array * @param array - The array to sort * @param key - The property key to sort by * @returns A new sorted array * * @example * ```ts * const items = [ * { value: 30 }, * { value: 10 }, * { value: 20 } * ]; * sortByNumber(items, 'value'); * // [{ value: 10 }, { value: 20 }, { value: 30 }] * ``` */ export function sortByNumber(array, key) { if (!Array.isArray(array)) { return []; } return [...array].sort((a, b) => { const valueA = Number(a[key]) || 0; const valueB = Number(b[key]) || 0; return valueA - valueB; }); } //# sourceMappingURL=sortByNumber.js.map