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 ]
I've done fixing the error in my writing. Again, thanks for pointing out.
Yeah, I totally forgot about that sort only based on lexographical order, and I even encounter this recently. Thanks for pointing that out 😁
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.
I've done fixing the error in my writing. Again, thanks for pointing out.
Yeah, I totally forgot about that sort only based on lexographical order, and I even encounter this recently. Thanks for pointing that out 😁