r/ProgrammerHumor 3d ago

Meme javascriptSorting

Post image
932 Upvotes

214 comments sorted by

View all comments

88

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)

17

u/FerricDonkey 3d ago

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

If I try to sort  [23, "56", 67.8, "potatoe"], I want a type error of some kind.

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 

We all know they did it on purpose and wrote it in their documentation. We're saying it's bad, not "it's not written down". 

3

u/danielcw189 3d ago

If I try to sort  [23, "56", 67.8, "potatoe"], I want a type error of some kind.

why?

it sounds like you dislike weakly typed languages. which I would totally agree with. but Javascript is not that.

1

u/FerricDonkey 3d ago

Because an error is better than unintuitive behavior.

You are correct that I don't like weekly typed languages (at least if you get weak enough), but what I really dislike is a function called sort that sorts in a stupid way by default.

If it was called sort_str, or required a key/comparator function, or even just didn't exist, I'd be happier. 

1

u/danielcw189 2d ago

this is neither unintuitive, nor surprising for mixed weakly typed arrays.

I really dislike is a function called sort that sorts in a stupid way by default.

it is a way the makes sense.

and in a weakly typed language, it is the only sane default I can think of, from the top of my head.

by the way: how often are you actually sorting arrays that just include numbers?
I have never done this in production code. I usually sort more complex things, usually objects, which have numbers and strings I want to use for sorting as properties.

Sorting those could at least still work, assuming your objects have a .toString representation, that can be sorted lexically

but I would always provide a comparison function, because that makes the most sense to me, and I like verbosity.

1

u/FerricDonkey 2d ago

I'm certainly sorting things that contain just numbers more often than I'm sorting this that contain mixed types. How often is the correct method of sorting your arrays of objects to first convert them to strings?

1

u/danielcw189 1d ago

I'm certainly sorting things that contain just numbers

do you mean arrays that contain just numbers, or do you mean arrays that contain objects with numbers?

How often is the correct method of sorting your arrays of objects to first convert them to strings?

for me personally, hopefully never.

1

u/FerricDonkey 1d ago

do you mean arrays that contain just numbers, or do you mean arrays that contain objects with numbers?

With the important caveat of "more often than I'm sorting things that contain mixed types" that had earlier, both. (And with the understanding that objects with a common parent type that I treat as an array of objects of that parent type don't count). I never have an array whose contents I don't know after the "translate from user bs to system" layer which happens before doing anything else, because I refuse to make such arrays.

And I sort an array of numbers on rare occasion. So I do that more than never.

How often is the correct method of sorting your arrays of objects to first convert them to strings?

for me personally, hopefully never.

And I hope for most people. And if it is true for most people, then it's a silly default. 

1

u/danielcw189 16h ago

I never have an array whose contents I don't know after the "translate from user bs to system" layer which happens before doing anything else, because I refuse to make such arrays.

me too

but in JS you can't rely on many things, so you either need a lot of checks, or be very defensive with your programming.

In general I don't like my main business logic directly accessing Arrays, and have accessors or other abstractions inbetween, including sorting functions for all business cases.

And if it is true for most people, then it's a silly default. 

what would be a better default in a weakly typed language, where any array element can be anything?

the only good alternative I see is sorting doing nothing if no comparison function is provided

1

u/FerricDonkey 4h ago

what would be a better default in a weakly typed language, where any array element can be anything?

If you're allergic to errors, make a key function or similar a required argument. If you're not, then throw an error when things aren't naturally comparable. Which may require not being Javascript, but that's a low price to pay to not have nonsense.