Sort an Array Alphabetically using the sort Method Error

Tell us what’s happening:
I have done everything I should for the test, but the test fails. I have rewritten the code in many ways, and have even copy-pasted the ‘hint solution’. I have reset the code.

What is going on?

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
}
console.log(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.132 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

just using sort on its own sorts in alphabetical order no need for any arguments

Right…Thanks.

I got caught up by the lesson saying it is best practice to always give a callback function.

1 Like
return arr.sort(function(a,b) {
   if(a > b) {
     return 1
   }else{
     return -1
   }
  });

this is how you would do it with callback function or…

return arr.sort((a, b) => a > b ? 1 : -1);
1 Like