Help Sort an Array Alphabetically using the sort Method

Tell us what’s happening:

the sort callback function isn’t working - i’m stuck… help please

Your code so far


function alphabeticalOrder(arr) {
    var newArr = arr.sort(function(a, b) {
       return a < b;
   })
   return newArr;
 };

 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/72.0.3626.121 Safari/537.36.

Well, actually the sort method expect a negative, positive or zero value, not a Boolean. It also has a default behaviour that may be interesting for you.

You can find the explanation of the method here:

If you have still doubts after reading it please ask again

1 Like

I got it, thanks for your help!