4 ms·
[6,-2,2,-7].sort() Result : [-2,-7,2,6] Ladies and Gentlemen, the lingua franca of web programming.
by pipeline_peak 2y ago
[6,-2,2,-7].sort()
Result :
[-2,-7,2,6]
Ladies and Gentlemen, the lingua franca of web programming.
- rk06 2y agoBecause js sort works on string. for numeric sort try: `[6,-2,2,-7].sort( (a,b) => a-b)`
- mock-possum 2y agoIt would be cool if an array of numbers was sorted by numeric value by default, instead of the values being sorted alphanumerically, yes, that’s a gotcha.
- leshenka 2y agonew Int32Array([2,7,-2, -7]).sort() Result: Int32Array [-7, -2, 2, 7] (4) The problem is you can put anything in array and JS can't really check that "everything here is a number", so it uses string as a common type because everything can be converted to string. we need proper typed arrays, like Int8/16/32Array but with any class, not just int. The class itself will then define how to compare instances (python's __gt__ and so on).
- evilduck 2y agoSomeone didn't read the docs.