One line solution and detailed explanation.
Intuition\n\nThe problem is about sorting an array arr based on a transformation function fn. Each element in arr is mapped to a numerical value through fn, and
## Intuition\n\nThe problem is about sorting an array `arr` based on a transformation function `fn`. Each element in `arr` is mapped to a numerical value through `fn`, and the sorting order depends on these transformed values. The function `fn` is consistent and won\u2019t duplicate numbers for the same elements in `arr`, making it a stable transformation for sorting.\n\nThe core idea is:\n1. Map each element in `arr` to a value using `fn`.\n2. Use these values to determine the order of elements in `arr`.\n\n---\n\n## Approach\n\nThe steps to solve this problem are as follows:\n\n1. **Transform Each Element with `fn`**:\n   Use the function `fn` to get a "key" (numerical value) for each element in `arr`. This key determines the order of the elements in the sorted array.\n\n2. **Use `.sort()` with Custom Comparator**:\n   JavaScript\'s `.sort()` allows us to specify a custom comparator function that determines the sorting logic. Here\u2019s how:\n   - For each pair of elements, `a` and `b`, in `arr`, we pass them to `fn`.\n   - We compare `fn(a)` with `fn(b)` using `fn(a) - fn(b)`. This produces:\n     - A negative result if `fn(a)` should appear before `fn(b)`.\n     - Zero if `fn(a)` and `fn(b)` are equal (though in this problem, the prompt guarantees unique numbers).\n     - A positive result if `fn(a)` should appear after `fn(b)`.\n\n3. **Return the Sorted Array**:\n   By using `.sort()` in this way, we sort `arr` based on the transformation values from `fn`.\n\n---\n\n## Complexity\n\n- **Time Complexity**:  \n  The `.sort()` method has a time complexity of \\( O(n log n) \\), where \\( n \\) is the length of `arr`. Calculating `fn(a)` for each element is constant time \\( O(1) \\), so the overall time complexity remains \\( O(n log n) \\).\n\n- **Space Complexity**:  \n  Since we are sorting `arr` in-place, the space complexity is \\( O(1) \\), assuming `fn` does not add any additional memory usage. However, if `fn` has its own memory requirements, the space complexity could increase.\n\n---\n\n## Code\n\n\n```javascript\n/**\n * @param {Array} arr\n * @param {Function} fn\n * @return {Array}\n */\nvar sortBy = function(arr, fn) {\n    return arr.sort((a, b) => fn(a) - fn(b));\n};\n```\n\n
['JavaScript']