I want to learn how the sort()
method works in JavaScript, I know that sort has a default function for comparison but I want to pass a function and understand how it processes the function.
I want to sort the following array ["a", "d", "c", "a", "z", "g"]
alphabetically, but when I use the method arr.sort((a,b) => a>b);
returns the same array without sorting.
Please, anyone can explain to me this.
I'm learning JavaScript and trying to understand the methods in the arrays, like map()
, reduce()
and filter()
but I get stuck in the sort()
method.
let arr = ["a", "d", "c", "a", "z", "g"];
console.log(arr.sort((a,b) => a>b));
the result is ["a", "d", "c", "a", "z", "g"]
, but I want ["a", "a", "c", "d", "g", "z"]
.
And I know if I use the sort()
method without arguments the algorithm sort it but I want to understand why it's not working with argument.