Given an array arr and a function fn, return a sorted array sortedArr. It can be assumed that fn only returns numbers, and these numbers determine the sorting order of sortedArr. sortedArr must be sorted in ascending order based on the output values of fn.
// Example 1
Input: arr = [5, 4, 1, 2, 3], fn = (x) => x
Output: [1, 2, 3, 4, 5]
Explanation: fn simply returns the input number, so the array is sorted in ascending order.
// Example 2
Input: arr = [{"x": 1}, {"x": 0}, {"x": -1}], fn = (d) => d.x
Output: [{"x": -1} , {"x": 0}, {"x": 1}]
Explanation: fn returns the value of the "x" key, so the array is sorted based on that value.
function sortBy(arr, fn){
return arr.sort((a,b)=> fn(a) - fn(b))
}