Returns a copy of the array, sorted by the value returned by calling the function in the second argument on each element in the array.
Examples: sortBy(['aaa', 'a', 'aa'], str => str.length) // ['a', 'aa', 'aaa'] sortBy(['aaa', 'a', 'aa'], str => -str.length) // ['aaa', 'aa', 'a'] sortBy([5, 3, 7], num => num) // [3, 5, 7] sortBy([5, 3, 7], num => -num) // [7, 5, 3] sortBy([bigint1, bigint2], x => x) // sorted bigints sortBy([obj1, obj2], obj => obj.prop) // sorted by property
The array to sort
Function returning the value to sort by
A new sorted array
Returns a copy of the array, sorted by the value returned by calling the function in the second argument on each element in the array.
Examples: sortBy(['aaa', 'a', 'aa'], str => str.length) // ['a', 'aa', 'aaa'] sortBy(['aaa', 'a', 'aa'], str => -str.length) // ['aaa', 'aa', 'a'] sortBy([5, 3, 7], num => num) // [3, 5, 7] sortBy([5, 3, 7], num => -num) // [7, 5, 3] sortBy([bigint1, bigint2], x => x) // sorted bigints sortBy([obj1, obj2], obj => obj.prop) // sorted by property
Param: array
The array to sort
Param: valueToCompare
Function returning the value to sort by
Returns
A new sorted array