Working on this challenge:
This is my code:
function alphabeticalOrder(arr) {
// Only change code below this line
return arr.sort(function(a, b) {
return a === b ? 0 : a < b ? -1 : 1;
});
// Only change code above this line
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
Which works and is all fine. But I don’t understand how it works.
How does this :
return a === b ? 0 : a < b ? -1 : 1;
tell the machine what to do?
Same question for the sort method applied to numbers. How does this code tell the machine how to sort them in ascending order?
return arr.sort(function(a, b) {
return a - b;
I read it a “minus” b. Is that what is going on? If so, how does that help the machine sort the numbers in ascending order?
Thanks to anyone who will help me understand