8 Replies Latest reply on Aug 3, 2007 8:00 AM by Newsgroup_User

    sortOn() problem

    mrSparkleS0ap
      I have an indexed array in a class that has a "seq" field that I want to sort by.

      I am using this:
      my_ar.sortOn("seq", Array.NUMERIC | Array.DESCENDING);

      and it returns the sequence like { 1,10,11,2,3,4,5,6,7,8,9}

      so it is sorting but not as Numeric??

      Is there an include file that I am missing?
        • 1. Re: sortOn() problem
          Level 7
          Hi,

          What's the original data? If these values are strings, numeric sorting
          will still do it according to string. Verify that your values are
          numbers and try again.

          Patrick

          mrSparkleS0ap wrote:
          > I have an indexed array in a class that has a "seq" field that I want to sort
          > by.
          >
          > I am using this:
          > my_ar.sortOn("seq", Array.NUMERIC | Array.DESCENDING);
          >
          > and it returns the sequence like { 1,10,11,2,3,4,5,6,7,8,9}
          >
          > so it is sorting but not as Numeric??
          >
          > Is there an include file that I am missing?
          >

          --
          http://www.baynewmedia.com
          Faster, easier, better...ActionScript development taken to new heights.
          Download the BNMAPI today. You'll wonder how you ever did without it!
          Available for ActionScript 2.0/3.0.
          • 2. Re: sortOn() problem
            mrSparkleS0ap Level 1
            Thanks...

            yeah they are strings. I fill the array with loaded XML.
            Is there a simple way to get it to work without having to modify my xml parsing class?
            Is there a way to change one value in multiple locations in a 2D array?
            • 3. sortOn() problem
              mrSparkleS0ap Level 1
              so I modified my XML parser to look like this

              var ndName:String = rootNode.childNodes .childNodes[x].nodeName;
              if (ndName == "seq") {
              part_ar[ndName] = Number(rootNode.childNodes
              .childNodes[x].firstChild.nodeValue);
              } else {
              part_ar[ndName] = rootNode.childNodes .childNodes[x].firstChild.nodeValue;
              }

              After I did this then sort like this
              my_ar.sortOn("seq", Array.NUMERIC | Array.DESCENDING);

              my array now orders like this { 1,2,3,6,7,8,9,10,4,5 }

              which is very odd.

              any ideas?

              • 4. Re: sortOn() problem
                Level 7
                This is getting closer! I have a feeling that your "else" statement is
                leaving some values as strings. Try making everything a number...no
                "if/else". Or...add a Number( ... conversion to the "else" statement as
                well.

                Patrick

                mrSparkleS0ap wrote:
                > so I modified my XML parser to look like this
                >
                > var ndName:String = rootNode.childNodes .childNodes[x].nodeName;
                > if (ndName == "seq") {
                > my_ar[ndName] =
                > Number(rootNode.childNodes
                .childNodes[x].firstChild.nodeValue);
                > } else {
                > my_ar[ndName] = rootNode.childNodes .childNodes[x].firstChild.nodeValue;
                > }
                >
                > After I did this then sort like this
                > my_ar.sortOn("seq", Array.NUMERIC | Array.DESCENDING);
                >
                > my array now orders like this { 1,2,3,6,7,8,9,10,4,5 }
                >
                > which is very odd.
                >
                > any ideas?
                >
                >
                >

                --
                http://www.baynewmedia.com
                Faster, easier, better...ActionScript development taken to new heights.
                Download the BNMAPI today. You'll wonder how you ever did without it!
                Available for ActionScript 2.0/3.0.
                • 5. Re: sortOn() problem
                  mrSparkleS0ap Level 1
                  I can't make everything a Number in this situation, but If I remove the Array.NUMERIC from the sortOn() it almost works.

                  my_ar.sortOn("seq", Array.NUMERIC );
                  will sequence the array like this { 11, 10, 9, 8 ,7 ,6 ,5 ,4 ,3 , 2 ,1 }

                  I just need it in the opposite order, but for some reason Array.DESCENDING has a problem with that.
                  • 6. Re: sortOn() problem
                    Level 7
                    Okay, then what you can do is simply my_ar.reverse(); after you do
                    everything else. That should put it into the correct order and you
                    should be able to make it work after that. However, mixing strings and
                    numbers this way will always cause the problems you're experiencing. The
                    string "10" is not the numeric value 10 so it won't be sorted in the
                    same way.

                    Good luck!

                    Patrick

                    mrSparkleS0ap wrote:
                    > I can't make everything a Number in this situation, but If I remove the
                    > Array.NUMERIC from the sortOn() it almost works.
                    >
                    > my_ar.sortOn("seq", Array.NUMERIC );
                    > will sequence the array like this { 11, 10, 9, 8 ,7 ,6 ,5 ,4 ,3 , 2 ,1 }
                    >
                    > I just need it in the opposite order, but for some reason Array.DESCENDING has
                    > a problem with that.
                    >
                    >

                    --
                    http://www.baynewmedia.com
                    Faster, easier, better...ActionScript development taken to new heights.
                    Download the BNMAPI today. You'll wonder how you ever did without it!
                    Available for ActionScript 2.0/3.0.
                    • 7. Re: sortOn() problem
                      mrSparkleS0ap Level 1
                      Thanks for your help.. But I decided that the sortOn function was a little too screwy for me.. So I created another array that stored the record sequence from the XML with its indexed location in my other Array. Then I just did sortOn() on that array and used it to index back into my original array. It works so I am happy. :-)

                      thanks again..
                      • 8. Re: sortOn() problem
                        Level 7
                        :) Now you're officially a Flash developer!

                        mrSparkleS0ap wrote:
                        > Thanks for your help.. But I decided that the sortOn function was a little too
                        > screwy for me.. So I created another array that stored the record sequence from
                        > the XML with its indexed location in my other Array. Then I just did sortOn()
                        > on that array and used it to index back into my original array. It works so I
                        > am happy. :-)
                        >
                        > thanks again..
                        >