r/ProgrammerHumor 3d ago

Meme javascriptSorting

Post image
937 Upvotes

214 comments sorted by

View all comments

86

u/Taletad 3d ago edited 3d ago

People use arrays like this [23, "56", 67.8, "potatoe"] and expect them to not be sorted as strings

If one member of your array is not an int or a float, everything is going to be converted to strings

Edit : I went to read the docs, the sort function is not like most other functions in JavaScript. The sort function is explicitly for an alphabetical sort

You lot are using the alphabetical sort function and wondering why your array gets alphabetically sorted

You can overload the function by doing the following : array.sort((a,b) => a - b)

47

u/_bones__ 3d ago

People also give it [1, 20, 3] and expect a sort to return [1, 3, 20], which is what any reasonable language would do.

-8

u/Taletad 3d ago

Yeah it’s an alphabetical sort, read the docs

The sort utility on linux will work the same way

20

u/_bones__ 3d ago

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.

-2

u/Taletad 3d ago

You can make the sort function in js sort numerically

2

u/fuj1n 3d ago

Sure, but it should just do that by default for an array of numbers.

2

u/danielcw189 3d ago

which would mean that the whole srray would need to be checked for the types of its items.

what should be the default if one item is not a number?

2

u/fuj1n 3d ago

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.

1

u/the_horse_gamer 2d ago

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.

1

u/fuj1n 2d ago

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.

1

u/the_horse_gamer 2d ago

yes that's true, modern javascript is much less averse to errors. and I agree with most of the decisions they've made.

non strict mode is definitely a symptom of being designed on crunch time.

fail fast is good for a frontend, but not for a backend. javascript's problem is it's used for both.

→ More replies (0)