133 points

Looks like they should have called that function sortOf()

permalink
report
reply
56 points

This is due to the default sorter in JavaScript sorting by the string value. The reason for that is that this won’t create errors at runtime. Since JavaScript does not have types, this is the safest way of sorting.

This works:

const unsorted = [1, 100000, 21, 30, 4]
const sorted = unsorted.sort((a, b) => a - b) 
permalink
report
reply
65 points

Which is a fine decision if you have a programming language to do silly stuff on a personal geocities page, but a horrible one when you start using that language to handle serious data. Silently ignoring what’s probably a bug is dangerous.

permalink
report
parent
reply
25 points

Which is a fine decision if you have a programming language to do silly stuff on a personal geocities page

And that is, of course, what it was designed for.

JS is brilliant considering that it was created by one dude in 10 days. Nobody thought it would become nearly as important as it has become.

permalink
report
parent
reply
11 points
*

Get out of here with your sensibility!

^kidding, ^js ^via ^ts ^is ^my ^life.

permalink
report
parent
reply
1 point
*

ah yes, a reasonable solution to that problem that any human would think of

ah yes, a reasonable problem that any human would think of – “what if someone tries to sort a list containing some integers and some arrays, and our interpreter needs to keep chugging instead of telling the programmer they fucked up?”

permalink
report
parent
reply
37 points
19 points

permalink
report
parent
reply
9 points

Based on this you can Take Shit even further with jsfuck

permalink
report
parent
reply
3 points
*

“Actually, this one isn’t ‘Wat’, it’s part of what makes Ruby awesome and powerful, unless of course you actually do this, at which point it’s ‘Wat’”

permalink
report
parent
reply
3 points
*

let’s talk about Ruby

Ruby like most programming languages doesn’t support bare words, [undefined variable exception]

but if you define a particular method_missing, suddenly Ruby supports bare words. [ruby repeating what was typed]

Now this isn’t deserving of wat. this actually shows just how awesome Ruby is. [Drummer_t-rex.jpg]

But if you actually do this then…

Wat

permalink
report
parent
reply
36 points

As a non-programmer, why does it do this? Sorting by leftmost digit seems super dumb.

permalink
report
reply
89 points
*
Deleted by creator
permalink
report
parent
reply
7 points

is there a good reason for javascript to work like that? python also isn’t typed like C and it sorts integer lists in the “normal” way, so it seems avoidable. (i don’t really know what im talking about here. i’ve never used javascript and i’m not familiar with how typing works under the hood.)

permalink
report
parent
reply
18 points

Mainly because JavaScript was designed to work along side HTTP in a browser. Most of its input will be text, so defaulting common behavior to strings makes some sense.

permalink
report
parent
reply
12 points
*

The hard part is sorting values of different types.

Python 2 had a order of built it types. Something like None < bool < numbers < strings. This means that you could sort anything like JavaScript and behaves fairly reasonably.

Python 3 takes the “safer” approach and comparisons of different types throw an exception. (You can override the comparison behavior for your own types and do something different).

JavaScript has conventionally been a very loosely typed language. So it almost certainly wouldn’t have chosen the exception throwing option. It does something similar to Python 2. But instead of just directly comparing the values of different types it converts them to strings first, then compares everything as a string. This is probably the most reasonable option. Otherwise you would have problems because 10 &lt; "2" and "2" &lt; 3 but 3 &lt; 10. How can that work? You have no total ordering! So basically because the comparison operators convert to strings if either argument is a string the default sort comparator really doesn’t have a choice but to do convert to string. The other option would be to define a total order just for the sort function but that seems more confusing.

permalink
report
parent
reply
3 points
*

It’s also an incorrect alphabetical sort in many languages that use accented characters. For a correct sort you need to pass it something like the localeCompare function or Intl.Collator.

permalink
report
parent
reply
18 points

You can put any type of value in an array in JavaScript. You can have numbers, strings, booleans, arrays, and objects. So what should the default sort function sort by? Sorting by numbers makes sense, but what if it wanted to sort strings instead?

When you don’t know what value is in an array ahead of time you can’t assume how to sort it. When you’re controlling the program you can provide a sort function for the type of values you know will be in it, but when you’re writing the standard default sort function, there’s only one type that you can convert all the other types to safely and simply in the most predictable way, which is strings.

permalink
report
parent
reply
15 points

"By default, the sort() function sorts values as strings.

This works well for strings (“Apple” comes before “Banana”).

However, if numbers are sorted as strings, “25” is bigger than “100”, because “2” is bigger than “1”.

Because of this, the sort() method will produce incorrect result when sorting numbers."

https://www.w3schools.com/js/js_array_sort.asp

permalink
report
parent
reply
8 points

It also produces incorrect results in many languages other than English. You can pass it a compare function that uses localeCompare or Intl.Collator to get a correct alphabetical sort.

permalink
report
parent
reply
14 points

Because it turns everything into text first. Just in case you try to sort [1,“apple”,45.99,false,[true,3]] rather than an array of similar things like a normal person.

permalink
report
parent
reply
9 points

Because when it’s sorting some of them as ints and some of them as strings. JavaScript has implicit conversion to string.

permalink
report
parent
reply
5 points

Wrong. JavaScript sort’s default comparison function always converts to strings.

permalink
report
parent
reply
3 points

Only if one of them is a string right? If you have only numbers then it works fine right? Right? (Please say that I’m right 😭)

permalink
report
parent
reply
9 points

Think of digits like you would letters. You’re essentially sorting numbers alphabetically. It’s not the right way to do it, of course, but it’s the natural way to do it using a system like computers use that doesn’t necessarily differentiate between digits and letters unless you tell it to specifically.

permalink
report
parent
reply
3 points

I think the main shortcoming here is that there isnt a way to specify the type to sort as, instead you have to write the function to compare them as numbers yourself. If it’s such a simple implementation, why isn’t it officially implemented? Why isn’t there a sortAs() that takes two args, the input list, and a Type value? Check every element matches the type and then sort, otherwise return a Type Error.

permalink
report
parent
reply
1 point
*

I mean, there’s a sort() method that takes a comparator(a,b) such that if a comes first it returns 1, if b comes first it returns -1 and if they’re equivalent wrt sortinf it returns 0. If you absolutely need type safe number sorting you can use that to get it.

permalink
report
parent
reply
1 point

it’s lexicographic order

permalink
report
parent
reply
1 point

It sorts them based on their unicode character, not the actual numbers. 1 is U+0031, 2 is U+0032, etc.

permalink
report
parent
reply
1 point
Deleted by creator
permalink
report
parent
reply
26 points

Because everyone means “alphabetize” when sorting numbers.

permalink
report
reply

Programmer Humor

!programmer_humor@programming.dev

Create post

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics

Community stats

  • 7K

    Monthly active users

  • 950

    Posts

  • 35K

    Comments