• @affiliate@lemmy.world
        link
        fedilink
        71 year ago

        is there a good reason for javascript to work like that? python also isn’t typed like C and it sorts integer lists in the “normal” way, so it seems avoidable. (i don’t really know what im talking about here. i’ve never used javascript and i’m not familiar with how typing works under the hood.)

        • @Quasari@programming.dev
          link
          fedilink
          English
          201 year ago

          Mainly because JavaScript was designed to work along side HTTP in a browser. Most of its input will be text, so defaulting common behavior to strings makes some sense.

          • @severien@lemmy.world
            link
            fedilink
            5
            edit-2
            1 year ago

            That’s misleading at best and most likely just false, and it’s worrying it’s so upvoted.

            There’s no historical record explaining why this was designed this way, but we can infer some things. HTTP is very unlikely a factor, XHR / AJAX has been added years after the .sort() function. Additionally, it doesn’t make sense in the context that other comparisons are not string-wise (sort()/quicksort is basically a series of comparisons).

            The trouble with JS arrays is that they can contain any values - e.g. [false, undefined, 1567, 10, "Hello world", { x: 1 }]. How do you sort those? There must be one function to compare every combination of value, but how do you compare booleans and objects?

            There’s no such function which would provide reasonable results. In that context, doing .toString() and then string-wise comparison/sorting doesn’t seem that crazy - every object has .toString(), it will compute something, and often it will work well enough.

            There could be some additional smartness - if the array contains numbers only, it could choose to use a number-wise comparison function. But that would require a) extra implementation complexity (JS was famously designed in short time) and b) reduced performance - since JS runtime doesn’t know what type of values are present in the array, it would have to scan the whole array before starting the sort. But I guess the a) was the decisive factor in the beginning and backwards compatibility prevented improving the function later.

            • @Quasari@programming.dev
              link
              fedilink
              English
              3
              edit-2
              1 year ago

              You are probably correct. I don’t know if it’s true, it’s probably more likely it was a way for it not to fail.

              I said HTTP mainly because HTML is plaintext because of it. 1.0s main purpose was to manipulate the page. Of course Array objects weren’t added til 1.1, when netscape navigator 3.0 released, but it was still mostly 1.0 code. I felt like having everything be coercable to string made it easy for you to just assign it to the document. If you assigned the wrong thing it wouldn’t crash.

              I originally thought there was a precursor to microsofts XMLHTTP in an earlier version due to the 1997 ECMAScript documentation specifically talking about using it both client and serverside to distribute computations, but it was far more static. So, I’m probably just wrong.

        • @kevincox@lemmy.ml
          link
          fedilink
          13
          edit-2
          1 year ago

          The hard part is sorting values of different types.

          Python 2 had a order of built it types. Something like None < bool < numbers < strings. This means that you could sort anything like JavaScript and behaves fairly reasonably.

          Python 3 takes the “safer” approach and comparisons of different types throw an exception. (You can override the comparison behavior for your own types and do something different).

          JavaScript has conventionally been a very loosely typed language. So it almost certainly wouldn’t have chosen the exception throwing option. It does something similar to Python 2. But instead of just directly comparing the values of different types it converts them to strings first, then compares everything as a string. This is probably the most reasonable option. Otherwise you would have problems because 10 &lt; "2" and "2" &lt; 3 but 3 &lt; 10. How can that work? You have no total ordering! So basically because the comparison operators convert to strings if either argument is a string the default sort comparator really doesn’t have a choice but to do convert to string. The other option would be to define a total order just for the sort function but that seems more confusing.

      • @floofloof@lemmy.ca
        link
        fedilink
        English
        3
        edit-2
        1 year ago

        It’s also an incorrect alphabetical sort in many languages that use accented characters. For a correct sort you need to pass it something like the localeCompare function or Intl.Collator.

    • @fidodo@lemm.ee
      link
      fedilink
      191 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.

    • @KiofKi@feddit.de
      link
      fedilink
      151 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
        81 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
      141 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
      121 year ago

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

        • @jormaig@programming.dev
          link
          fedilink
          41 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
            5
            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}` &lt; `${b}` ? -1 : 1).

    • Alien Nathan Edward
      link
      fedilink
      101 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.

      • @CheezyWeezle@lemm.ee
        link
        fedilink
        31 year ago

        I think the main shortcoming here is that there isnt a way to specify the type to sort as, instead you have to write the function to compare them as numbers yourself. If it’s such a simple implementation, why isn’t it officially implemented? Why isn’t there a sortAs() that takes two args, the input list, and a Type value? Check every element matches the type and then sort, otherwise return a Type Error.

        • Alien Nathan Edward
          link
          fedilink
          1
          edit-2
          1 year ago

          I mean, there’s a sort() method that takes a comparator(a,b) such that if a comes first it returns 1, if b comes first it returns -1 and if they’re equivalent wrt sortinf it returns 0. If you absolutely need type safe number sorting you can use that to get it.

          • @CheezyWeezle@lemm.ee
            link
            fedilink
            21 year ago

            Right, but you have to make that comparator yourself, it’s not a built-in part of the language. The only built-in comparator converts values to strings and compares them in code units orders.

            Also, that technically isnt type-safe, is it? If you threw a string or a NaN at that it would fail. As far as I knew, type safe means that a function can handle type errors itself, rather than throwing an exception. So in this case the function would automatically convert types if it was type-safe to prevent an unhandled exception.

            • Alien Nathan Edward
              link
              fedilink
              21 year ago

              Not every use case can be the built-in default. I wouldn’t have made JS weakly typed if I were designing it, but once the decision was made to use weak typing it made sense to either have no default sort method or to have a default sort method that assumes a type.

              What I’ve outlined for you is the interface for a comparator, not the implementation. You can type check and convert and do anything else you want under the hood of the comparator you write.

              • @CheezyWeezle@lemm.ee
                link
                fedilink
                21 year ago

                It doesn’t have to be the default to be built in, tho. It could be an overloaded function, having the “default” be the typical convert-to-string sorting, and an overloaded function that allows to specify a type.

                It’s just such a common thing, wanting to sort a list by different types, that I’m surprised there hasn’t been an official implementation added like this. I get that it a simple “fix” to make, but I just think that if it’s that simple yet kind of obscure (enough that people are still constantly asking about it) there should be an official implementation, rather than something you have build yourself.

                • Alien Nathan Edward
                  link
                  fedilink
                  21 year ago

                  Thats just JS for you. If you’re being generous, it’s a “quirky” language. If you’re being ungenerous, it’s a steaming pile of arbitrary decisions, gotchas, unexpected behaviors and problems that no one bothered to solve because there’s a workaround.

                  • @CheezyWeezle@lemm.ee
                    link
                    fedilink
                    11 year ago

                    Yeah, JS always seemed like the red-headed stepchild of modern languages. I’d be curious to know if other ECMAScript languages like JScript are as, eh, “quirky”, suggesting that the ECMA spec is the source of the quirkiness, or if JavaScript itself is the one making silly decisions. Technically, I mostly work with Google’s AppScript when I use ECMAScript stuff, but I’m fairly certain AppsScript is based off of JavaScript instead of directly based on the ECMA spec, so I don’t think it’s separate enough for me to draw a conclusion there.

    • @nero@lemmy.world
      link
      fedilink
      31 year ago

      It sorts them based on their unicode character, not the actual numbers. 1 is U+0031, 2 is U+0032, etc.