• Konlanx@feddit.de
    link
    fedilink
    arrow-up
    1
    ·
    1 year 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) 
    
    • AVincentInSpace@pawb.social
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      11 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?”

  • nintendiator@feddit.cl
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    Honestly this being javascript I expected the answer to be

    [4, 1, 100000, 30, 21]
    

    (sorted alphabetically by name)

    • StarkillerX42@lemmy.ml
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      1 year ago

      “Actually, this one isn’t ‘Wat’, it’s part of what makes Ruby awesome and powerful, unless of course you actually do this, at which point it’s ‘Wat’”

      • Zeragamba@lemmy.ca
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        1 year ago

        let’s talk about Ruby

        Ruby like most programming languages doesn’t support bare words, [undefined variable exception]

        but if you define a particular method_missing, suddenly Ruby supports bare words. [ruby repeating what was typed]

        Now this isn’t deserving of wat. this actually shows just how awesome Ruby is. [Drummer_t-rex.jpg]

        But if you actually do this then…

        Wat