# Callback function not working: Sort an Array Alphabetically using the sort Method

Tell us what’s happening:

The explanation for this challenge seems like it’s trying to get me to use a callback function to pass, but when I do it returns the array exactly as it was input with no sorting applied. My code matches the solution provided in the hint but it still doesn’t pass. However, when I write it as just return arr.sort(); with no callback function it passes just fine, which seems to go against the whole point of the explanation. Any help understanding what’s going wrong is much appreciated!

``````
function alphabeticalOrder(arr) {
return arr.sort(function(a,b) {
return a > b;
});
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36`.

2 Likes

That makes sense! So the lesson example is incorrect?

``````function reverseAlpha(arr) {
return arr.sort(function(a, b) {
return a < b;
});
}
reverseAlpha(['l', 'h', 'z', 'b', 's']);
// Returns ['z', 's', 'l', 'h', 'b']
``````

I’m going to have to eat some crow here. I was WRONG. Sorry, I was trying to do this from a phone in a waiting room and so very obviously did not read your post completely.

I think your code should have worked just fine. I’m not sure why it is not passing. Now that I’m sitting in front a real computer I’ve put your original solution through its paces and I’m getting the correct answers for each test.

The passing solution is to offer NO callback and let the default Javascript sort put your array in alphabetical order.

2 Likes

No problem! Yeah, I tried looking into it before I posted it and I was confused because the hint solution and other forum posts seemed to do it the way I did. I think something with fcc is glitched somehow and keeping it from passing. Thanks for all your help!

The “correct” solution is not working for me either on this challenge. When I run it locally and log results, it doesn’t sort at all. I also pasted in the reverse alpha sort function from the challenge example:

``````function reverseAlpha(arr) {
return arr.sort(function(a, b) {
return a < b;
});
}
reverseAlpha(['l', 'h', 'z', 'b', 's']);
// Returns ['z', 's', 'l', 'h', 'b']
``````

and that doesn’t sort as claimed either (using latest version of Chrome). The point of this challenge (and stated in the Note is to use a callback, so leaving it out and allowing the default unicode sorting may work on the test but I don’t think it is what is intended for the challenge.

A solution that DOES work for me is:

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

Would be nice to get some definite clarification as to whether the example provided in the challenge (and the proposed solution for regular alpha sort are incorrect completely…?

thanks,
Steve

There was a bug in this challenge introduced by later versions of Chrome. That is why some campers are reporting passing answers in some other environment but not being able to pass the challenge in the FCC environment.

There is an issue open for this bug in github. The example and hint solution will be updated soon to reflect the new requirements to pass the challenge

The new correct example looks very much like yours! So good work on that. sort() should require that your compare function return 1, -1 or 0 but in older versions of Chrome evidently true or false could be used as well.

2 Likes

Many thanks for that reference alhazen1! Very interesting for me to see the bug identification and discussion on Github like that, as I am just starting to learn GH as well. Appreciate the time you took to respond.

Steve

1 Like

Thanks for posting I have been following the example and couldn’t figure out for half an hour want went wrong