const numbers = [74, 18, 10, 5, 84, 24, 105]
numbers.sort(compareFunction)
console.log(numbers)
function compareFunction(a, b) {
return a - b
}
The above is sorting the array as a numerically ascending order, not as a string, but how does it do unless I understand I won’t be able to apply that in other situations in the future?
In total there are 7 elements in the array, holding positions from 0 - 6.
- element position at “0” will be compared to the remaining, 1-6?
- Once Point No 1 is accomplished then the element at position 1 will be compared to the remaining elements occupying positions at JS index between 2-6? and so on…
- Finally sorted array is delivered.