sort
@guoba-ai/utils / array / sort
Function: sort()
function sort<T>(
array,
fn,
desc?): T[];Defined in: array.ts:237
Sort an array by a derived numeric value. Returns a new array.
Parameters
array
T[]
The array to sort
fn
(item) => number
Function that maps each item to a number for comparison
desc?
boolean
If true, sort in descending order
Returns
T[]
A new sorted array
Example
sort([3, 1, 2], v => v) // [1, 2, 3]
sort([3, 1, 2], v => v, true) // [3, 2, 1]
const input = [{ score: 2 }, { score: 1 }]
sort(input, item => item.score) // [{ score: 1 }, { score: 2 }]
input // [{ score: 2 }, { score: 1 }]