alphacyberranger@sh.itjust.works to Programmer Humor@programming.dev · vor 1 JahrI'll just sort it myselfsh.itjust.worksimagemessage-square48fedilinkarrow-up1926arrow-down112
arrow-up1914arrow-down1imageI'll just sort it myselfsh.itjust.worksalphacyberranger@sh.itjust.works to Programmer Humor@programming.dev · vor 1 Jahrmessage-square48fedilink
minus-squarekevincox@lemmy.mllinkfedilinkarrow-up5·vor 1 JahrWrong. JavaScript sort’s default comparison function always converts to strings.
minus-squarejormaig@programming.devlinkfedilinkarrow-up4arrow-down1·vor 1 JahrOnly if one of them is a string right? If you have only numbers then it works fine right? Right? (Please say that I’m right 😭)
minus-squarekevincox@lemmy.mllinkfedilinkarrow-up5·edit-2vor 1 JahrNo. It always compares by converting to string. I actually think this is more consistent then having different behaviour if you have a string somewhere in your list. Basically the default comparator is a.sort((a, b) => `${a}` < `${b}` ? -1 : 1).
Wrong. JavaScript sort’s default comparison function always converts to strings.
Only if one of them is a string right? If you have only numbers then it works fine right? Right? (Please say that I’m right 😭)
No. It always compares by converting to string. I actually think this is more consistent then having different behaviour if you have a string somewhere in your list.
Basically the default comparator is
a.sort((a, b) => `${a}` < `${b}` ? -1 : 1)
.