Sort an Array Alphabetically using the sort Method-not working

Tell us what’s happening:

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/75.0.3770.142 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

It says it’s recommended to put in a function, but by default the sort method sorts the array alphabetically so you don’t need to put in a function

@LiChoi Got it !!
Thanks.

I’m assuming you copied the hint; the hint is wrong in Chrome as of October last year; it’s been updated but the new version of the hint isn’t live on the site yet.

The callback function you’ve specified is not correct, that’s why it doesn’t work – it needs to return a negative number, zero, or a positive number, not just true or false.

As @LiChoi says, just using the default with no callback function works (and this is actually what the hint has been changed to). Note it does not sort alphabetically, it sorts by string code values, which is something different (here are the first 255 code point values for example: https://www.rapidtables.com/code/text/ascii-table.html, that’s what it sorts by).

In this case the sort order is alphabetical because all the characters are lowercase letters in the English alphabet (try adding some uppercase letters, & you’ll see it won’t be alphabetical – it’ll order all the uppercase letters alphabetically first followed by all the lowercase letters alphabetically).

2 Likes

@DanCouper, there is a clear explanation of the answer in the question and I assumed the answer to the problem would be similar to that(That’s what they might be excepting).
When there is a hint to all the problems, what is the purpose of the forum ?
I really don’t prefer the way you are addressing problems here. Be more suggestive rather than being considerate.

Everyone assumed that when they went through the challenge the first time.

You can say hint or example code, it’s the same, it’s wrong.

The update of the curriculum has been delayed, and we can’t do anything else other than point out that the callback you think is the solution doesn’t work because browser developers changed the support of the method. When the last website update will finish with Q&A the hint and example code will change

1 Like

Since this is the only thread here for this challenge. It is still not working or maybe broken in a different way.

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

Should be correct and works in the browser console/everywhere else.

Edit: Works in Firefox, fails in Chrome

No, that’s the issue I’m describing above, it’s due to the sort algorithm in Chrome being changed last year; whether it is really correct depends on how the ECMAScript spec is interpreted, Chromium took a different view of it than Mozilla. There are, I would say twenty or thirty threads highlighting the issue; the hint has been amended and has been for a while, but it is a slow and complex process getting new versions of FCC out (it isn’t as simple as just changing a bit of text), so unfortunately the hint that is currently visible is still there.