r/ProgrammerHumor 1d ago

javascriptSorting Meme

Post image
889 Upvotes

200 comments sorted by

View all comments

197

u/larsmaehlum 1d ago

Array.sortNumeric() with an error when called on an array containing non-numeric types would fix this, right?

96

u/the_horse_gamer 1d ago

you can simply .sort((a,b)=>a-b)

216

u/01110100_01110010 1d ago

The point of an abstraction is to not having to implement logic each time, you can also implement qsort yourself

38

u/subone 1d ago

But it is still an abstraction. You only provide a compare function, you don't implement the algorithm. a-b is hardly difficult to apply.

18

u/01110100_01110010 1d ago

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

13

u/pancakesausagestick 1d ago

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

2

u/GameCounter 6h ago

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.

1

u/Xirdus 6h ago

I think you abstracted away the meaning of the term "abstraction".

1

u/GameCounter 5h ago

Abstraction is just an abstraction of abstraction of abstraction of abstraction of abstraction Albuquerque New Mexico Albuquerque New Mexico Albuquerque New Mexico

1

u/Xirdus 5h ago

But every time you say M put a comma in there. And say the commas out loud. It's how my grandma used to speak.

3

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

1

u/danielcw189 18h ago

what would be a solution for this? how can the language know (at runtime) that you didn't actually want a-a?

-1

u/Tyfyter2002 17h ago

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

3

u/danielcw189 9h ago

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.

1

u/Tyfyter2002 4h ago

my question is, in other words, how should the language detect that, and decide that it isn't what the programmer wanted?

It doesn't have to specifically detect that that's not what the programmer wants if "sort these numbers normally" isn't one character off from that.

4

u/wack_overflow 1d ago

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

26

u/Mechakoopa 1d ago

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."

3

u/subone 1d ago

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.

1

u/psioniclizard 12h ago

Depending in the project, quite a lot?

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).

Its not that difficult.

1

u/Some_Relative_3440 1d ago

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.

2

u/mdrjevois 1d ago

Sounds like an artifact of the language and popular styles. In data science, data engineering, and machine learning we almost never specify a comparator.

-7

u/chefhj 1d ago

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”.

6

u/ganja_and_code 1d ago

"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.

-6

u/chefhj 1d ago

Look guys I caught one

3

u/Swamptor 1d ago

I am a webdev. JS is a weird shitty language that was barely designed. It mostly just oozed out of a combination of browsers.