914
I'll just sort it myself (sh.itjust.works)
you are viewing a single comment's thread
view the rest of the comments
[-] LanternEverywhere@kbin.social 36 points 1 year ago

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

[-] fidodo@lemm.ee 18 points 1 year 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.

load more comments (26 replies)
this post was submitted on 05 Sep 2023
914 points (98.7% liked)

Programmer Humor

19572 readers
824 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