Functional Programming - Sort an Array Alphabetically using the sort Method (a topic in lecturing)

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

reverseAlpha(['l', 'h', 'z', 'b', 's']);

The code given in the lecture in step 17 of the “Functional Programming” (Sort an Array Alphabetically using the sort Method) course;
return a === b ...
Shouldn’t the line be resolved as I have indicated below?

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

Note: Your attention should be on -1 and 1. :point_left:
Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: Functional Programming - Sort an Array Alphabetically using the sort Method

Link to the challenge:

If you have an and b being actually equal to letter a and letter b then your method will sort them in normal order instead of reverse order.

Since a is less than b, your code returns -1 and placed a before b (so that is not reverse)

1 Like

Thank you, I think it was my lack of knowledge on this subject, I was in a hurry, thank you

1 Like

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