• kevincox@lemmy.ml
    link
    fedilink
    arrow-up
    5
    ·
    10 months ago

    Wrong. JavaScript sort’s default comparison function always converts to strings.

    • jormaig@programming.dev
      cake
      link
      fedilink
      arrow-up
      4
      arrow-down
      1
      ·
      10 months ago

      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 😭)

      • kevincox@lemmy.ml
        link
        fedilink
        arrow-up
        5
        ·
        edit-2
        10 months ago

        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).