Okay, so i understand how to sort numbers from big to small or small to big. what I don't understand is how it actually is done by javascript. can somebody walk me through, step by step how javascript is sorting the following array? I know how to use it, I need to understand it.
var arr = [3, 2, 4, 5, 1]
function sorter(a, b) {
return a - b;
}
arr.sort(sorter)
can you show me which number is for a, and which is for b, and how does it iterate through the rest of the array. For instance, 3 - 2 is 1, so it would switch 2 to an index lower than 3, how does this continue for this entire array? I want to understand why this works, not just use it blindly. Thanks!!