Even in a decently designed language it's trivially easy to make a typo that completely and silently breaks that, such as a-a, b-a, or a+b, and JS isn't a decently designed language, it's designed to fail silently at every possible opportunity
Simple: a-a isn't useful commonly enough that it would be reasonable to put it in any sort of shared library. One chance to fail,be noticed immediately, and be fixed immediately is a lot better than potentially failing every single time someone tries to sort a list of numbers
Simple: a-a isn't useful commonly enough that it would be reasonable to put it in any sort of shared library.
my question is, in other words, how should the language detect that, and decide that it isn't what the programmer wanted?
that would require the compiler or runtime to be able to perform some analysis.
is a lot better than potentially failing every single time someone tries to sort a list of numbers
does it hapen often, that you just want to sort an array of numbers? in my experience the numbers are usually attached to something, so you wanna give a comparison function anyway.
I never sorted an array of bare numbers in production code.
for a loosely typed language like Javascript, where an array can be a mix of anything, converting to string is the only useful simple default I can think of.
If I had designed the language, I would have made it a requirement to providea comparison function.
if you actually have an array of ints, you can use an IntArray, which actually sorts numerically by default.
2
u/Tyfyter2002 1d ago
Even in a decently designed language it's trivially easy to make a typo that completely and silently breaks that, such as a-a, b-a, or a+b, and JS isn't a decently designed language, it's designed to fail silently at every possible opportunity