#2724: Sort By
IntuitionThe main idea behind solving this problem is to leverage JavaScript's built-in sort() method and customize it to sort elements based on a computed valu
# Intuition
The main idea behind solving this problem is to leverage JavaScript's built-in `sort()` method and customize it to sort elements based on a computed value using a function. The `sort()` method allows flexibility in defining sorting criteria by passing a comparator function.

# Approach
1. The `sortBy` function takes two parameters: an array `arr` and a function `fn`.
2. It applies the `sort()` method on the array and provides a custom comparator function.
3. The comparator function takes two arguments, `a` and `b`, and uses `fn(a)` and `fn(b)` to determine their relative order.
4. The comparator returns the difference between `fn(a)` and `fn(b)` to sort the array in ascending order based on the computed values.
5. The original array is modified and returned as the sorted array.

# Complexity
- **Time Complexity**:
  - The sorting operation has a time complexity of $$O(n \log n)$$, where \(n\) is the size of the input array.
  - Applying the function `fn` to each element during the comparison adds a constant-time overhead, which does not change the overall complexity.

- **Space Complexity**:
  - The space complexity is $$O(1)$$ additional space since the sorting is performed in place. However, the `sort()` method may use internal stack space for recursion, which depends on the JavaScript engine implementation.

# 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']