Sort method explanation

Tell us what’s happening:
hey guys,
I need someone to explain to me this line of code below!

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

only this line above!

Your code so far

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

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0.

Challenge: Sort an Array Alphabetically using the sort Method

Link to the challenge:

Do you understand what a ternary expression is? If not, you should review the FCC challenges which deal with the ternary operator.

1 Like

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

this is what is known as a ternary expression;
It is similar to an if-else statement.

for example - the ternary expression as an if-else statement is read as:

if(a === b){
return 0

} else if (a < b) {
return -1
} else {
return 1

1 Like