Sort an Array Alphabetically using the sort Method Question

Tell us what’s happening:

I’m not sure why this code won’t work.

Your code so far

function alphabeticalOrder(arr) {
// Add your code below this line

return arr.sort((a,b) => (a > b))

// Add your code above this line
}
alphabeticalOrder([“a”, “d”, “c”, “a”, “z”, “g”]);

The example in the challenge description is deprecated

Check this:

1 Like

Is there a way for me to make it work in this context?

if you want to sort chars or strings then you don’t need to pass a function to the sort() method, because the role of this method is to sort strings and if we want to sort number we pass a function to bypass this issue because .
By default, the sort() function sorts values as strings .

I see. What if you want to change the sort order of the strings (ascending vs. descending)? How would you do that if you don’t pass in a function?

The function passed to sort has to return one of three values (-1, 0, or 1), not true/false (check the examples in the documentation @ilenia posted).

This has been gone over a lot on the forums (the example using > stopped working in Chrome in a out October), and there is a fix in place but it hasn’t gone live as far as I know.

Also, may help (do search the forums though, there are a lot of threads on this issue and a lot of good answers which explain what sort does, how it works etc)

I used this for passing through.

function alphabeticalOrder(arr) {
  return arr.map((e) => e.charCodeAt(0))
            .sort((a, b) => a - b)
            .map((e) => String.fromCharCode(e));
}