🍞🍞🍞 1 LINE | Full thorough explanation | ✅ bread
Approach\nFor an array arr, if we call arr.sort(), it will automatically sort the array. However, JavaScript won\'t automatically sort correctly, since how we w
# Approach\nFor an array ```arr```, if we call ```arr.sort()```, it will automatically sort the array. However, JavaScript won\'t automatically sort correctly, since how we want the array to be sorted is not solely based on the value of each element, but rather the result of each element being inputted into a function. However, we can create a custom sort, where the parameter is the function we use to sort.\n\nWe create a custom sort in this format:\n```\narr.sort((a, b) => [expression])\n```\nThe ```sort``` function will constantly swap values based on our custom sort function. If the function\'s return value is negative, ```a``` should go to the *left* of ```b```. If the return value is positive, ```a``` should go to the *right* of ```b```. The custom sort function we want is simple and is as follows:\n```\n(a, b) => fn(a) - fn(b)\n```\nIf ```fn(a)``` is less than ```fn(b)```, then the function will return a negative value and ```a``` will go before ```b```. If ```fn(a)``` is greater than ```fn(b)```, the function will return a positive value, and ```a``` will go after ```b```.\n\n# Code\n```\nvar sortBy = function(arr, fn) {\n    return arr.sort((a, b) => fn(a) - fn(b));\n};\n```\nUPVOTE if this was helpful \uD83C\uDF5E\uD83C\uDF5E\uD83C\uDF5E
['Array', 'Sorting', 'JavaScript']