Why is this not passing? (Sort an Array Alphabetically using the sort Method)

<%= @topic_view.topic.title %>
<%= @topic_view.topic.average_rating %> <%= @topic_view.topic.posts.count { |p| !!p.custom_fields['rating'] } %>

Tell us what’s happening:
I don’t understand why this is not passing. The test cases are somehow failing here, although they are giving desired results when I am trying them out on my PC. Any help?

Your code so far


function alphabeticalOrder(arr) {
  // Add your code below this line
    return arr.sort(function(a,b) {
    return a > b;
  });
  
  // Add your 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/76.0.3809.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method

Not sure why it would fail in Chrome. It passes on Firefox though

Chrome doesn’t seem to support anymore that way of sorting
the callback needs to return negative/zero/ positive for it to work

I am in the functional programming section, doing the sort() challenge.
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method

It’s easy, and even though everything was alright with the code (as far as I can tell), It was simply not passing. I checked the hint, it was exactly the same as my code. I even tried copy-pasting the hint code, in order to bypass some flaw I might have been overlooking, but even that is not working. Any help?

PS : I even tried out the test cases that were failing somehow, and all of them gave the desired results on my end.

What does your code look like?

Almost identical to the one provided in the hints section

function alphabeticalOrder(arr) {
  // Add your code below this line
    return arr.sort(function(a,b) {
    return a > b;
  });
  
  // Add your code above this line
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);

Hmm I don’t know why the hint shows that. I would only compare the sort parameters if I’m dealing with numbers. But with letters, you can just simply call .sort(), as that will automatically sort out each letter alphabetically.

Lmao using sort with no callback function worked like a charm! Yes, the hint was weird. Made another post regarding a hint that was potentially outright wrong, but it did not gain much attention. Anyway, thanks :slight_smile:

No problem! Yeah sometimes the hint can be a little strange… because if you compare if a > b and vice versa does not output the same result that the hint is giving. If you ever need to sort it from descending, you can call sort on the variable and then chain it with reverse.