Need help breaking down this code

Could someone please help me break down this code? I understand it can be used to sort elements alphabetically. But I don’t understand how the operator works in this context.

return arr.sort(function(a, b) {
    return a === b ? 0 : a < b ? -1 : 1;
  });
if (a === b) {
  return 0;
} else if (a < b) {
  return -1;
} else {
  return 1
}

Nested ternaries, as used in that code, are extremely difficult to read, they’re normally to be avoided.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.