Fabio Manganiello on Nostr: npub1lhngm…ss0mj for sake of completeness: #JavaScript Array.sort function by ...
npub1lhngm68szjfcqr6yptylfnupq3dyajl7h56j05zdwhllf7e0ezdsgss0mj (npub1lhn…s0mj) for sake of completeness: #JavaScript Array.sort function by default calls toString on all the items before sorting, thus the sort comparator is the string comparator.
If your array really contains only numbers (who would have think of such a corner case, right?), then you’ll have to explicitly pass a sort comparator:
❯ [1, 11, 4, 3, 2].sort((a, b) => a - b)
Array(5) [ 1, 2, 3, 4, 11 ]
If your array really contains only numbers (who would have think of such a corner case, right?), then you’ll have to explicitly pass a sort comparator:
❯ [1, 11, 4, 3, 2].sort((a, b) => a - b)
Array(5) [ 1, 2, 3, 4, 11 ]