this post was submitted on 05 Sep 2023
914 points (98.7% liked)

Programmer Humor

18255 readers
723 users here now

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

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 18 points 10 months ago

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.