DEV Community

Discussion on: Javascript - Into The Loop of Arrays

Collapse
 
mayvid14 profile image
Mayvid14

As far as I am aware, the JavaScript array.sort method does not sort numbers in ascending or descending order if no function is passed to it. It sorts them in lexicographic order. So we have to pass a function to sort a numerical array.

[ 8, 4, 10, 1 ].sort();
// Output: [ 1, 10, 4, 8 ]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
alserembani profile image
Atif Aiman

I've done fixing the error in my writing. Again, thanks for pointing out.

Collapse
 
alserembani profile image
Atif Aiman

Yeah, I totally forgot about that sort only based on lexographical order, and I even encounter this recently. Thanks for pointing that out 😁