UNPKG

diginext-utils

Version:
34 lines 923 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sortByNumber = sortByNumber; /** * 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 }] * ``` */ 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