• @[email protected]
    link
    fedilink
    5610 months ago

    This is due to the default sorter in JavaScript sorting by the string value. The reason for that is that this won’t create errors at runtime. Since JavaScript does not have types, this is the safest way of sorting.

    This works:

    const unsorted = [1, 100000, 21, 30, 4]
    const sorted = unsorted.sort((a, b) => a - b) 
    
    • @[email protected]
      link
      fedilink
      6610 months ago

      Which is a fine decision if you have a programming language to do silly stuff on a personal geocities page, but a horrible one when you start using that language to handle serious data. Silently ignoring what’s probably a bug is dangerous.

      • @[email protected]
        link
        fedilink
        2510 months ago

        Which is a fine decision if you have a programming language to do silly stuff on a personal geocities page

        And that is, of course, what it was designed for.

        JS is brilliant considering that it was created by one dude in 10 days. Nobody thought it would become nearly as important as it has become.

    • @[email protected]
      link
      fedilink
      English
      1
      edit-2
      7 months ago

      ah yes, a reasonable solution to that problem that any human would think of

      ah yes, a reasonable problem that any human would think of – “what if someone tries to sort a list containing some integers and some arrays, and our interpreter needs to keep chugging instead of telling the programmer they fucked up?”