Comment on I'll just sort it myself

<- View Parent
severien@lemmy.world ⁨1⁩ ⁨year⁩ ago

That’s misleading at best and most likely just false, and it’s worrying it’s so upvoted.

There’s no historical record explaining why this was designed this way, but we can infer some things. HTTP is very unlikely factor, XHR / AJAX has been added years after the .sort() function.

The trouble with JS arrays is that they can contain any values - e.g. [false, undefined, 1567, 10, “Hello world”, { x: 1 }]. How do you sort those? There must be one function to compare every combination of value, but how do you compare string and object?

The simple answer to that is .toString() - every object has it, it will compute something, and often it will work well enough.

source
Sort:hotnewtop