Menu

why does sort() put 10 before 9 on a number array

Reading 0:00
15s threshold

i had an array of 10, 9, 1, 100 and called sort() on it and got 1, 10, 100, 9. i assumed it would just handle numbers. passing function(a, b) { return a - b } fixes it, but i don't really get why the default does this. is it converting everything to strings first?

and if so why would they design it that way, it seems like the least useful default possible.

submitted by /u/CelebrityChess
[link] [comments]