85 m/s runtime
IntuitionThe problem requires sorting an array based on the values returned by a given function fn. Since fn only returns unique numbers for different elements,
# Intuition
The problem requires sorting an array based on the values returned by a given function fn. Since fn only returns unique numbers for different elements, we can use a simple sorting algorithm that compares these numerical outputs. JavaScript’s built-in .sort() method provides an efficient way to achieve this.

# Approach
Use .sort():
.sort((a, b) => fn(a) - fn(b)) ensures ascending order sorting.
fn(a) and fn(b) return numerical values that dictate the order.
Since the numbers are unique, no need to handle duplicates.
Sorting Logic:
If fn(a) < fn(b), the element a comes before b.
If fn(a) > fn(b), the element b comes before a.
This approach works for numbers, objects, or nested arrays, as long as fn extracts a valid numeric key.

# Complexity
Time Complexity:
Sorting operation: The .sort() method in JavaScript uses Timsort, which has an average and worst-case complexity of O(n log n).
Function calls: The function fn() is called for each element, adding an extra O(n) overhead.
Overall complexity: O(n log n).

Space Complexity:
.sort() performs in-place sorting, meaning it does not require extra space beyond the input array.
The function fn() may create temporary values but does not store extra copies of the array.
Overall space complexity: O(1) (if sorting in place) or O(n) (if the sorting algorithm creates a copy internally).


# Code
```javascript []
/**
 * @param {Array} arr
 * @param {Function} fn
 * @return {Array}
 */
var sortBy = function(arr, fn) {
    return arr.sort((a, b) => fn(a) - fn(b));
};

```
['JavaScript']