alphacyberranger to Programmer [email protected] • 2 years agoI'll just sort it myselfsh.itjust.worksimagemessage-square48fedilinkarrow-up1928arrow-down112
arrow-up1916arrow-down1imageI'll just sort it myselfsh.itjust.worksalphacyberranger to Programmer [email protected] • 2 years agomessage-square48fedilink
minus-square@[email protected]linkfedilink9•2 years agoBecause when it’s sorting some of them as ints and some of them as strings. JavaScript has implicit conversion to string.
minus-square@[email protected]linkfedilink5•2 years agoWrong. JavaScript sort’s default comparison function always converts to strings.
minus-square@[email protected]linkfedilink3•2 years agoOnly 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-square@[email protected]linkfedilink5•edit-22 years agoNo. 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).
Because when it’s sorting some of them as ints and some of them as strings. JavaScript has implicit conversion to string.
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)
.