I’m not sure why I’m not getting these letters to ascend in order. I’m using the sort method with the arrow function.
function alphabeticalOrder(arr) {
// Only change code below this line
return arr.sort((a,b) => a - b);
// Only change code above this line
}
console.log(alphabeticalOrder(["a", "d", "c", "a", "z", "g"]));
No. You can, but you would be recreating the default sorting callback function if you did so.
There is an example in the lesson on how to sort in reverse alphabetical order. If you want to use a callback function, I’d try to work through how that one works.
Or, you can build your own based upon these three rules for the callback function’s return value
If the return value is 0, the two elements are the same/are equal
If the return value is negative, then the first element comes before the second element
If the return value is positive, then the second element comes before the first element
function alphabeticalOrder(arr) {
// Only change code below this line
arr.sort((a,b)=>{
if(a<b){
return -1
}else if (a>b){
return 1
}else{
return 0
}
});
}
// Only change code above this line
console.log(alphabeticalOrder(["a", "d", "c", "a", "z", "g"]));