Functional Programming: Sort an Array

Tell us what’s happening:
Could anyone explain me what is going on here? I don’t get the

 return a === b ? 0 : a < b ? 1 : -1;

part in the reverse array. What does b ? 0 : a < b ? 1: -1 mean?

Your code so far


function alphabeticalOrder(arr) {
// Only change code below this line


// Only change code above this line
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Sort an Array Alphabetically using the sort Method

Link to the challenge:

The function given to sort has to return -1, 0 or 1.

return a === b ? 0 : a < b ? 1 : -1

Same as

if (a === b) {
  return 0;
} else if (a < b) { 
  return 1;
} else {
  return -1;
}
1 Like