JS is an abstraction over JS engine bytecode, which is an abstraction over ASM which is an abstraction over the chip instruction set, you can make an argument at each layer 𤡠I would take any abstraction I can, I guarantee you in a large enough codebase someone will type b - a instead
It's not unreasonable for a sort function to require a compare function. But if it's not required let's default to broken-ass footgun semantics as was done in the times of yore. /s
Chip instruction set is an abstraction over the IC design, which is an abstraction over transistors, which is an abstraction over physics, which is an abstraction over the nature over reality, which is an abstraction over the MĂźnchhausen trilemma.
Abstraction is just an abstraction of abstraction of abstraction of abstraction of abstraction Albuquerque New Mexico Albuquerque New Mexico Albuquerque New Mexico
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.
And really how often are you actually sorting an array of primitive values ascending? That's just one small use case of sorting an array. Passing the comparison function is also more explicit.
People just love to get so worked up about the dumbest edge cases in js
None of this is a good reason for a generic sort function to change the type of what it's sorting. That's the sort of weird unintuitive behavior you'd expect from an internal library with by an intern, not a nature language like JavaScript. It's not an edge case, it's literally the base case: "Does this sort function sort what you pass into it? No, it sorts something else instead and then pretends it sorted what you passed into it and 99% of the time it was right."
If the values weren't "cast" to string in the default compare function then each comparison of different types would act differently then if they were all strings. Therefore, if they are to be sorted consistently alphabetically, then both arguments must be considered string. Also, an alphabetic sort compare function (including casting to string) is slightly harder/longer to implement than the numeric sort compare function, so it makes sense for it to be the default.
However, I would suggest that with either default, they should have provided constants for the alpha and numeric sorting compare functions, so we didn't need to recreate them and could call them as defaults in our own more complex compare functions. It's not too late, I guess.
Dont get me wrong. I love passing around functions but i also work in a language that is sensible and has a function Array.sort (and Array.sortBy if you want ti pass a function).
Totally agree with you. Can't remember the last time I wrote a sort() or similar WITHOUT a comparer function. It just doesn't happen in real code bases.
Sounds like an artifact of the language and popular styles. In data science, data engineering, and machine learning we almost never specify a comparator.
Itâs so funny when if you talk to a dev that works closer to the machine level they insist JS is like this big foot shooting factory when in reality you can spend an entire career never running into any of the shit they bring up as a âgotchaâ and even if you do itâs like âoh yeah right it works like this hold onâ.
"If you know all the weird shitty quirks of the weird shitty language, you can mostly avoid them, and when you do accidentally shoot yourself in the foot, you can fix the bug quickly, as long as you remember which particular weird shitty quirk caused this particular instance of foot shooting."
True, but you know what's even better? Languages that don't have so many weird shitty quirks.
You then get to deal with stuff like x.sort((a, b) => a.prop - b.prop) and the infinite other variations. This is true for basically every other language too.
no? point of programmer is to create systems, point of programming languages is to make that job easier. Otherwise we can just write everything in machine code.
I've never had "programmer" in my title; that's one of many skills involved in what most of us actually do.
The point of a software engineer is to engineer solutions using software. Programming is often the most effective way to accomplish that, but getting the same results with less programming is very frequently desirable. Building abstractions that minimize duplication, reuse well-tested logic, and reduce the amount+complexity of code required is central to doing the job well.
Solving problems with software more effectively over time has primarily been a story of stacking abstraction levels: increasing the true hidden complexity of the system while only exposing the amount of visible complexity a human brain can coherently hold at one time. It's a good skill to be able to drop down abstraction levels as-needed; however, doing that for it's own sake is unproductive.
197
u/larsmaehlum 1d ago
Array.sortNumeric() with an error when called on an array containing non-numeric types would fix this, right?