this post was submitted on 05 Sep 2023
917 points (98.7% liked)
Programmer Humor
19623 readers
69 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
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
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 < "2"
and"2" < 3
but3 < 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.