Sort an Array Alphabetically using the sort Method -- Bug? OFFICIAL solution does NOT work

Tell us what’s happening:
Below is the official solution, and it is quite easy to figure out from the example. In addition, this works in my ide fine- yet does not pass. After searching the forums further down is the solution that works.

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"]);

Code that works:

function alphabeticalOrder(arr) {
// Add your code below this line
var newArr = arr.sort();
console.log(newArr);
// Add your code above this line
return newArr;
}
alphabeticalOrder

Your browser information:

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

I agree with you. I think the official answer is not correct for this one.

For alphabet sorting, no argument needs to be given.
It is only required for number sorting.
If we really want to use the compare function, it should return numeric value anyway.

e.g.
function alphabeticalOrder(arr) {
return arr.sort((a,b)=>{
if(a>b){
return 1
} else {
return -1
}
})
}

1 Like

It is a browser issue it seems

Check this other topic

1 Like