Looks like the elements are sorted in ascending, ASCII character order. If we provide a compare function, then the elements are sorted based on the compare function.
If compareFn is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order. For example, "banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in the Unicode order. All undefined elements are sorted to the end of the array.
const numarr = [23,245,23,35,4,234];
console.log(numarr.sort());
why the above sort result in alphabetical sorting order instead of number based sorting order?
Looks like the elements are sorted in ascending, ASCII character order. If we provide a compare function, then the elements are sorted based on the compare function.
If compareFn is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order. For example, "banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in the Unicode order. All undefined elements are sorted to the end of the array.
developer.mozilla.org/en-US/docs/W...
yes it converts them to string then sort that's why we need to manually pass the compare function