The example code given for this problem is no longer correct and fails to sort the array in recent browsers:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method/
function reverseAlpha(arr) {
return arr.sort(function(a, b) {
return a < b;
});
}
console.log(reverseAlpha(['l', 'h', 'z', 'b', 's']));
// Returns ['z', 's', 'l', 'h', 'b']
It returns the original unsorted array.
A correct example would be:
function reverseAlpha(arr) {
return arr.sort(function(a, b) {
if (a < b) return 1;
if (b < a) return -1;
return 0;
});
}
reverseAlpha(['l', 'h', 'z', 'b', 's']);
// Returns ['z', 's', 'l', 'h', 'b']
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 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/