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)
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.
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?"
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.)
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.
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.
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.
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.
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.
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.
The picture isn't completely correct. JavaScript sorts numbers just fine. But if those were strings, that's exactly how JavaScript would order those. It compares character by character instead of doing a lookahead which is much more resource intensive. If you want it to look ahead, you have to tell it how to, else it defaults to the order in the image.
It's sad to have the rushed ramblings of a bigot become the fundamental block of the modern world wide web. Why couldn't it be at least made by a more competent bigot like Carmack?
As many people have pointed out already, this happens because JavaScript was rushed. But why do we still use a language whose foundation was built in only ten days(!) for scripting on webpages we build today? Why hasn't there been a push for web browsers to support other scripting languages (other than maybe Dart)?