How to sort the array like this ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • abs

    #1

    How to sort the array like this ?

    ar = new Array()
    ar = {"key1":"value1 ", "key2":"val ue2, "key3":"value3" };

    Anybody knows how to sort such arrray by values ? I've tried writing the
    comparing function and than
    ar.sort(myCompa reFunction)
    but there appears an error: "ar.sort is not a function"

    Best regards,
    ABS


  • Michael Winter

    #2
    Re: How to sort the array like this ?

    On 20/03/2005 09:31, abs wrote:
    [color=blue]
    > ar = new Array()[/color]

    This creates an empty array object.
    [color=blue]
    > ar = {"key1":"value1 ", "key2":"val ue2, "key3":"value3" };[/color]

    This creates an initialised object, and overwrites the previously
    assigned array.

    You need to make up your mind: do you want an array, or an object?
    [color=blue]
    > Anybody knows how to sort such arrray by values ?[/color]

    It doesn't make sense to sort an object which has non-numeric indicies
    because you can't enumerate an object in any guaranteed order.

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • abs

      #3
      Re: How to sort the array like this ?

      Michael Winter wrote:[color=blue]
      > This creates an initialised object, and overwrites the previously
      > assigned array.[/color]

      Oh, ok, now I know.
      [color=blue]
      > You need to make up your mind: do you want an array, or an object?[/color]

      Array.
      [color=blue]
      > It doesn't make sense to sort an object which has non-numeric indicies
      > because you can't enumerate an object in any guaranteed order.[/color]

      Understood. Thank you very much.

      ABS


      Comment

      • Michael Winter

        #4
        Re: How to sort the array like this ?

        On 20/03/2005 10:22, abs wrote:
        [color=blue]
        > Michael Winter wrote:[/color]

        [snip]
        [color=blue][color=green]
        >> You need to make up your mind: do you want an array, or an object?[/color]
        >
        > Array.[/color]

        I forgot to mention, array literals use square brackets. However,
        there is no way to specify the indicies:

        var array = ['a', 'b', 'c', 'd'];
        array[1] // 'b'

        That said, you can skip indicies by omitting a value:

        var array = ['a', 'b', , 'd', 'e'];

        [snip]

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Douglas Crockford

          #5
          Re: How to sort the array like this ?

          > ar = new Array()[color=blue]
          > ar = {"key1":"value1 ", "key2":"val ue2, "key3":"value3" };[/color]

          You are missing a close quote on "value2. It is a good idea to run
          JSLint on sample code before posting. See http://www.JSLint.com
          [color=blue]
          > Anybody knows how to sort such arrray by values ? I've tried writing the
          > comparing function and than
          > ar.sort(myCompa reFunction)
          > but there appears an error: "ar.sort is not a function"[/color]

          You should only use arrays when all of the keys are whole numbers,
          right? What you want is an object. Objects are unordered, but you can
          access the members in sorted order.

          <html><body><pr e><script>
          var ar, i, key, ob, va;
          ob = {"key1":"value1 ", "key2":"value2" , "key3":"value3" };

          // Make an array of keys and an array of values.

          ar = [];
          va = [];
          for (key in ob) {
          ar.push(key);
          va.push(ob[key]);
          }

          // Show sorted by key

          ar.sort();
          for (i = 0; i < ar.length; i += 1) {
          key = ar[i];
          document.writel n(key + ' : ' + ob[key] + ', ');
          }

          // Show sorted by value

          va.sort();
          for (i = 0; i < ar.length; i += 1) {
          key = va[i];
          document.writel n(va[i] + ', ');
          }
          </script></pre></body></html>


          Comment

          • Csaba Gabor

            #6
            Re: How to sort the array like this ?

            Michael Winter wrote:[color=blue]
            > On 20/03/2005 09:31, abs wrote:
            >[color=green]
            >> ar = {"key1":"value1 ", "key2":"val ue2, "key3":"value3" };[/color]
            >
            > It doesn't make sense to sort an object which has non-numeric indicies
            > because you can't enumerate an object in any guaranteed order.
            >[/color]

            Hey Mike!

            Did you ever come across an object where the enumeration
            order was not in the order of key creation (subject to
            key removals, etc.)? In both PHP and Javascript I don't
            recollect ever having run across such a situation, but
            this is a point I've always been curious about.

            Csaba Gabor from Vienna

            Comment

            • Michael Winter

              #7
              Re: How to sort the array like this ?

              On 20/03/2005 19:32, Csaba Gabor wrote:

              [snip]
              [color=blue]
              > Did you ever come across an object where the enumeration order was
              > not in the order of key creation (subject to key removals, etc.)?[/color]

              No, I haven't. However, I have access to a very limited subset of user
              agents so that fact that I haven't means absolutely nothing.

              [snip]

              Mike

              --
              Michael Winter
              Replace ".invalid" with ".uk" to reply by e-mail.

              Comment

              • Douglas Crockford

                #8
                Re: How to sort the array like this ?

                > Did you ever come across an object where the enumeration[color=blue]
                > order was not in the order of key creation (subject to
                > key removals, etc.)? In both PHP and Javascript I don't
                > recollect ever having run across such a situation, but
                > this is a point I've always been curious about.[/color]

                The ECMAScript Language Specification says of the object:

                It is an unordered collection of properties each of which
                contains a primitive value, object, or function. [4.3.3]

                While an implementation might have so order (such as order of key
                creation), there is no guarentee that the order is stable. Also,
                programs that rely on a particular order will fail on other implementations .


                Comment

                Working...