4 ms·
Why? Sorting strings is a far more common thing to do than just sorting numbers, so it makes sense to use simple string comparisons as the default algorithm. Ra
by tjansen 13y ago
Why? Sorting strings is a far more common thing to do than just sorting numbers, so it makes sense to use simple string comparisons as the default algorithm. Rather than come up with something that first evaluates the types in the list and then tries to figure out what to do.
- dsl 13y agoNo, it makes sense to sort strings when you get handed strings, and sort numbers when handed numbers. Period. Default behavior is now exactly as expected by 100% of developers. Your creativity as a language designer should only come in to play when dealing with sets of mixed type.
- Yaggo 13y agoSo how should [1, "a", 2, 3] be sorted by default?
- integraton 13y agoBoth Python and Erlang would return [1,2,3,"a"], Ruby and Clojure won't allow the comparison. Either of these approaches makes more sense than JavaScript's.
- bpicolo 13y agoJavascript returns [1,2,3,a]. What's the problem.
- integraton 13y ago> [1,2,"10",3].sort() [ 1, '10', 2, 3 ] That's the problem.