Js Easy
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires sorting an array based on a custom comparison function. The idea i
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires sorting an array based on a custom comparison function. The idea is to use JavaScript\'s built-in Array.prototype.sort() function and customize the comparison logic using the function fn provided as an argument.\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUse the sort method:\n\nThe sort method is a natural choice for sorting an array as it allows custom comparator functions.\nCustom Comparator:\n\nFor each element in the array, apply the provided function fn.\nUse the result of fn to determine the sorting order.\nSpecifically, the comparator fn(a) - fn(b) calculates the difference between the results of fn applied to two elements, a and b, determining their order.\nReturn the Sorted Array:\n\nAfter sorting, the modified array is returned.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\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```
['JavaScript']