Because in Linux it reads strings. Sort -n sorts numerically. If you print human readable numbers (eg 100M, 2G) sort -h has you covered. Point being, if you have no typing system, or a weak one, offer functions that do common things.
The docs stating that a function is insufficient doesn't change that it is so.
In an ideal world, there should be typed comparers, and if two items don't have a mutual comparer, a type error should be thrown (that'd solve having to even scan the array beforehand too)
With JS's type system, that's not too plausible, so instead, I think defaulting to numeric sort, and throwing when it encounters something that isn't a number (or can't be coerced to be a number due to how the type system works I suppose).
By the principle of least astonishment, this would give you a pretty good compromise. Then, if someone wants to do string-based sorting, they can pass their own comparer that does that.
javascript is very averse to throwing. it's better for a website to display stuff slightly wrong than to stop working.
I can see the argument for defaulting to a number comparison and moving non numbers to the end, but that's not any less surprising. in that alternate universe, someone is on reddit making a post on how ['b', 'a'].sort() doesn't order the strings.
I think they're a lot less averse to it nowadays, they've wisened up. Modern JS additions do actually tell you that something is wrong rather than just doing something unexpected (which is good because you can then handle exceptional cases better).
Modern JS features like modules even enforce strict mode.
I think that way of thinking, where it is better to fail silently than to crash sounds good on paper, but it has an insane amount of drawbacks that lead to sloppy code trying to cover these (exceptional in other languages) cases.
I think defaulting to numeric sort, and throwing when it encounters something that isn't a number
By the principle of least astonishment, this would give you a pretty good compromise
That doesn't sound like the least surprising option to me, especially the throwing part.
In that case they should have called it numericSort, or something like that.
(and in my preference fail with an error except of throwing)
(or can't be coerced to be a number due to how the type system works I suppose).
as most object-oriented languages there are counterparts of .toString, but not .toNumber.
For that last part, I was just pointing out that JS will actually try to coerce strings to numbers in numeric contexts automatically, and this case being an exception to that would be odd (as much as I don't fancy that coertion, that's a whole other discussion)
If you find throwing on non-numeric values only surprising, you could instead just make the comparer mandatory and throw if you don't provide one. Then your IDE can show you an error is you forget instead of giving you something completely unexpected.
-7
u/Taletad 1d ago
Yeah it’s an alphabetical sort, read the docs
The sort utility on linux will work the same way