• Carlos Solís@communities.azkware.net
      link
      fedilink
      arrow-up
      3
      ·
      1 year ago

      It’s sad to have the rushed ramblings of a bigot become the fundamental block of the modern world wide web. Why couldn’t it be at least made by a more competent bigot like Carmack?

    • StarkillerX42@lemmy.ml
      link
      fedilink
      arrow-up
      3
      ·
      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
        3
        ·
        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

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

    Honestly this being javascript I expected the answer to be

    [4, 1, 100000, 30, 21]
    

    (sorted alphabetically by name)

  • Konlanx@feddit.de
    link
    fedilink
    arrow-up
    5
    ·
    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) 
    
    • jmcs@discuss.tchncs.de
      link
      fedilink
      arrow-up
      5
      ·
      1 year 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.

      • seitanic@lemmy.sdf.org
        link
        fedilink
        arrow-up
        5
        ·
        1 year 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.

    • 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?”

    • fidodo@lemm.ee
      link
      fedilink
      arrow-up
      4
      ·
      1 year ago

      You can put any type of value in an array in JavaScript. You can have numbers, strings, booleans, arrays, and objects. So what should the default sort function sort by? Sorting by numbers makes sense, but what if it wanted to sort strings instead?

      When you don’t know what value is in an array ahead of time you can’t assume how to sort it. When you’re controlling the program you can provide a sort function for the type of values you know will be in it, but when you’re writing the standard default sort function, there’s only one type that you can convert all the other types to safely and simply in the most predictable way, which is strings.

    • Alien Nathan Edward@lemm.ee
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      Think of digits like you would letters. You’re essentially sorting numbers alphabetically. It’s not the right way to do it, of course, but it’s the natural way to do it using a system like computers use that doesn’t necessarily differentiate between digits and letters unless you tell it to specifically.

    • KiofKi@feddit.de
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      "By default, the sort() function sorts values as strings.

      This works well for strings (“Apple” comes before “Banana”).

      However, if numbers are sorted as strings, “25” is bigger than “100”, because “2” is bigger than “1”.

      Because of this, the sort() method will produce incorrect result when sorting numbers."

      https://www.w3schools.com/js/js_array_sort.asp

      • floofloof@lemmy.ca
        link
        fedilink
        English
        arrow-up
        1
        ·
        1 year ago

        It also produces incorrect results in many languages other than English. You can pass it a compare function that uses localeCompare or Intl.Collator to get a correct alphabetical sort.

    • Blackmist@feddit.uk
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      Because it turns everything into text first. Just in case you try to sort [1,“apple”,45.99,false,[true,3]] rather than an array of similar things like a normal person.

    • jormaig@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      1 year ago

      Because when it’s sorting some of them as ints and some of them as strings. JavaScript has implicit conversion to string.

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

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

        • jormaig@programming.dev
          link
          fedilink
          arrow-up
          1
          ·
          1 year 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
            2
            ·
            edit-2
            1 year 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).

  • yetAnotherUser@lemmy.ca
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    As many people have pointed out already, this happens because JavaScript was rushed. But why do we still use a language whose foundation was built in only ten days(!) for scripting on webpages we build today? Why hasn’t there been a push for web browsers to support other scripting languages (other than maybe Dart)?

    • Kayn@dormi.zone
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 year ago

      There has never been a push because JavaScript works well enough.

      Many of its mistakes have been rectified in later specifications.