Sorting an Array Alphabetically with sort

Tell us what’s happening:

I am not quite sure why my solution isn’t working…I run my solution in vscode vs the test cases and I return what it says should be returned. Could someone point me to where I am going wrong, or if this challenge is broken… Thanks!!

Your code so far


function alphabeticalOrder(arr) {
    return arr.sort( (a, b) => {return a > b;})
}


alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 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/

EDIT: I guess its broken, the solution in the hint doesnt pass either.

The callback function should return -1, 0 or 1, yours does not do that. The hint was correct for all browser up until Chrome adjusted how sort worked in October last year (and still works fine in other browsers), there is a fix to edit the hint but it isn’t live yet. The fix for you is to return the correct thing from the function you give to sort, it currently isn’t correct.

1 Like

Thanks for the different browser tip! My solution, and the hit worked in safari. :slight_smile:

1 Like

I would suspect that the way you’ve done it will, going foward, break in all browsers, so have a check on the current solution here:

https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method.english.md

And have a search on the forums - this has come up a lot because of the hint code not working in Chrome (and the fact that the solution worked fine up until recently, so if you google around you’ll find a lot of people using this method to sort), here’s one of my earlier answers:

1 Like