Sort an Array Alphabetically using the sort Method 2

Tell us what’s happening:
The code is not giving the desired result even though it is the correct solution

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 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 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

1 Like

Since the arrays only contain lowercase characters you could just return arr.sort(), the default sort method works ok in that case.

But the reason that your compare function is not working is because it is returning true or false and the sort function wants to see positive number or negative number in order to properly sort. I believe the true will be coerced to 1 and the false will be coerced to 0. Returning 0 will result in no sort action.

4 Likes

This seems to be because of a fallacy pushed as fact on the lesson portion (it is already fixed on github ? I think). If you paste their reverse sort into the page and check the array using dev tools you can see that it doesn’t work either. This is a problem with the lesson and their listed solution in the hints section. You followed the lesson to its logical conclusion. Good job.

To get the correct answer you could try “return a.localeCompare(b);” inside the callback function. This is a string function that returns the desired positive, negative, and zero results regardless of the language.

2 Likes

Thanks so much for helping understand what must be the callback function return in Array.prototype.sort(). In order to follow the faulty FCC instructions as closely as possible, I did this very simple hack:

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

Just substracting any number bellow 1 should do the job.

1 Like

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Yes, this is a known issue and I believe it is being addressed in the next edition of FCC. FCC is build by volunteers and mistakes happen.

Your solution, yes it’s a bit hacky. Basically you are converting true into .9 and every ‘false’ into -.1 with kind of works - the sort method expects either a positive or negative number to tell it how to sort those two elements. The issue with your hack is that sort expects a 0 to be returned if they are the same. I think it will still work but in some implementations of the sorting algorithm, it might be less efficient. (I don’t know for sure, but I know that in some sorting algorithms this can be important.)

The best solution is to handle all three cases. Return 1 if it’s bigger or -1 if it’s smaller. If it gets through, that means that they are the same, in which case you can return 0. I believe that if you want you can just return nothing, which will be undefined and falsy so it will coerce to 0. But explicitly returning 0 is probably better.