Can someone explain me step by step what is this line doing:
return a === b ? 0 : a < b ? -1 : 1;
Your code so far
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"]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.
Challenge: Sort an Array Alphabetically using the sort Method
? 0 if a === b returns truthy, then it’s going to return 0
: a < b If a === b returns falsey, then it’s gonna ask again if a < b
? -1 If a < b returns truthy, then it’s going to return -1
: 1 If a < b returns falsey, then it’s going to return 1
/* this is called a Ternary operator. It is a shorter version of
If() block. It's used if you know the test is only return truthy (true) or falsey (false) */
test ? truthy : falsey