Sort() Method Not Working

I’m on the Functional Programming section of JavaScript. For some reason, the sort method is not working on the assignment, “Sort an Array Alphabetically using the Sort Method.” I’m not sure what the issue is, but when I console it, it’s not returning anything. And it’s only with sort().

Hi @bluesatch, welcome to the forums.
You need to include which lesson you’re working on and show your code otherwise any response you get will be a guess.

If you click on the ‘Ask for Help’ button it will do most of it for you.

Functional Programming: Sort an Array Alphabetically using the sort Method

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

result of return in sort() must be zero or negative or positive to determine position of element in array.
Ref: When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.

If using return a > b -> it will be always true or false, and we can not sort.
if using return a - b -> we can not do this with String, but Number.
You can see:

console.clear()
console.log("a" > "b" > 0) // false
console.log("a" > "b" < 0) // false
console.log("a" > "b" === 0) // false


console.log( 3 - 2 > 0) // true
console.log(3 - 2 < 0) // false
console.log(3 - 2 === 0) // false

Resolution: Default with string, sort() will sort alphabetically
arr.sort()
If reverse: arr.sort().reverse()