Simple Solution with explanation of the program and approach to solve the problem step by step
IntuitionUnderstand the given condition and think the logic to the given problem and the return value.ApproachWe just have to return the sorted array.Program Br
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Understand the given condition and think the logic to the given problem and the return value.
- - -
# Approach
<!-- Describe your approach to solving the problem. -->
We just have to return the sorted array.
- - -
# Program Breakdown
` arr` is a array which we have to sort. `fn` is a function that return a number which we pass to it.
- - -
# Program Breakdown
**THIS IS A BREAKDOWN OF THE PROGRAM GIVEN BELLOW**
- We have to retun a sorted array so we use only 1 line to sort and return the array by using in-buit function ` sort()`.
- `return arr.sort((a,b)=>fn(a)-fn(b))` which sort and return the array.
- `arr.sort()` sort the array based on the arrow functin `(a,b)=> fn(a) - fn(b)`.where `a` and `b` are the arguments represent the 2 elemens of the `arr` array.
- `fn(a) - fn(b)` return a number which may be a positive or negetive number. If it return a positive number it sort `b` first and then `a`. If it return a negetive number it sort `a` first and them `b`.This contuinue ntil all elements are over in `arr` array.
- - -
# Complexity
- Time complexity: $$O(n$$ $$log$$ $$n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
The time complexity of the `sort` method in JavaScript is generally O(n log n) on average, where n is the number of elements in the array. This is because the sorting algorithm used (typically a variation of quicksort or mergesort) has an average-case time complexity of O(n log n)
- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
The space complexity of the `sort` method is O(n) in the worst case due to the need for additional space for the sorting algorithm's operations. 
- - -
# Code
```javascript []

var sortBy = function(arr, fn) {
    return arr.sort( (a, b) => fn(a) - fn(b))
};


```
- - -
['JavaScript']