A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [RESOLVED] sortOn a NUMERIC Array

  1. #1
    Senior Member
    Join Date
    Sep 2007
    Location
    UK
    Posts
    194

    resolved [RESOLVED] sortOn a NUMERIC Array

    Hopefully someone can help me out with this...

    I have a button on the screen with the the following code attached to it's release function.

    Actionscript Code:
    completeList = new XML();
    completeList.ignoreWhite = true;
    completeList.onLoad = function(success) {
        if (success) {
            tcRecordings.removeAll();
            mainheadings = this.firstChild.childNodes;
            var dataTag:Array = new Array();
            for (var a = 0; a<mainheadings.length; a++) {
                newSub = this.firstChild.childNodes[a].childNodes;
                for (var i = 0; i<newSub.length; i++) {
                    //tcRecordings.addItem(newSub[i].attributes.mydata+" - "+newSub[i].attributes.mylabel,[newSub[i].attributes.mydata]);
                    dataTag.push({label:newSub[i].attributes.mydata, data:newSub[i].attributes.mydata});
                }
            }
            dataTag.sortOn("data",Array.NUMERIC);

            tcRecordings.dataProvider = dataTag;
        }
    };

    This should hopefully populate a List Component with the appropriate data. The only issue comes from the fact that the XML mydata information is numeric ranging from 0 through 10313.

    My XML looks like this:

    <Index mydata="8229" mylabel="Osteoarthritis" ></Index>
    No matter how i sort the dataTag array it never puts the information into the respective order. The 10XXX figures always come ahead of the preceeding numerals.

    Anyone come across this before who can point me in the right direction?

    Mike
    Last edited by michaeltowse; 08-20-2010 at 07:49 AM.

  2. #2
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    reverse the for loop?

    gparis

  3. #3
    Senior Member
    Join Date
    Sep 2007
    Location
    UK
    Posts
    194
    I have no idea how to go about that gparis lol

    The loop itself runs through a series of top level category nodes to populate the information from within. Bit like the below...

    PHP Code:
    <database>
    <
    system mydata="0" mylabel="Click here to select a Category.....">
    </
    system>
    <
    system mydata="1" mylabel="Cat1">
    <
    Index mydata="6904" dateStr="FIRST ONE" orderInt="0" mylabel="Course Title" ></Index>
    <
    Index mydata="7063" dateStr="June 2110" orderInt="1" mylabel="Course Title" ></Index>
    </
    system>

    <
    system mydata="2" mylabel="Cat2">
    <
    Index mydata="7635" dateStr="June 2010" orderInt="12" mylabel="Course Title" ></Index>
    <
    Index mydata="8962" dateStr="June 2010" orderInt="8" mylabel="Course Title" ></Index>
    <
    Index mydata="9074" dateStr="June 2010" orderInt="11" mylabel="Course Title" ></Index>
    <
    Index mydata="9913" dateStr="June 2010" orderInt="10" mylabel="Course Title" ></Index>
    </
    system>

    <
    system mydata="3" mylabel="Cat3">
    <
    Index mydata="10298" dateStr="June 2010" orderInt="9" mylabel="Course Title" ></Index>
    </
    system>



    </
    database

  4. #4
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    for (var a = mainheadings.length; a>0; a--)

    gparis

  5. #5
    Senior Member
    Join Date
    Sep 2007
    Location
    UK
    Posts
    194
    that works as much as the results show backwards but the results still don't show as I would expect them to. The 10XXX now show at the bottom but not in the order i had hoped....

  6. #6
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    ok then i didn't understand what the problem was. I thought you meant 10298 came before 9913. Or is it something else?

    gparis

  7. #7
    Senior Member
    Join Date
    Sep 2007
    Location
    UK
    Posts
    194
    Thanks for looking at the code gparis, i've loaded my current FLA and XML to the attached zip in case it helps shed a little light to the confusion i seem to be having....

    Thanks again

    Mike
    Attached Files Attached Files

  8. #8
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    I'm writing this just to subscribe so I can see the outcome. Michael, I downloaded your zip and the the application is doing what it seems it's supposed to other than It's not pulling some of the data from the other nodes when you select the dropdown. But, that looks like it's because it's looking for xml files not in the zip. So, I'm completely baffled as to what sort order you're looking for as gparis has hinted at: 10298 is coming after 9913 so maybe you are wanting something else?

    in the fla You had:
    dataTag.sortOn("data",Array.NUMERIC);
    dataTag.sort(Array.DESCENDING);

    you can do:
    dataTag.sortOn("data", Array.NUMERIC | Array.DESCENDING);
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

  9. #9
    Senior Member
    Join Date
    Sep 2007
    Location
    UK
    Posts
    194
    Hi mneil, thanks for taking the file down for a look. I've attached another XML and FLA just incase I took a little too much from the original one.

    At times the 10*** items do come after the others, but the list should run from smallest number to biggest number. When the 10*** are last in the list, the 9*** are first when it should be the 6*** first followed by 7 etc etc

    I've tried variations hence the commented out code and extra node data.

    I really want the data to run in a sequential order from lowest number to highest when the blue button is clicked.

    I'm confident that I am just missing a step, or a few marbles.....
    Attached Files Attached Files

  10. #10
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    We all have a few marbles loose! So I looked and looked at this thing trying to figure it out. I rewrote it a bit and still nothing. I stared at the results, and stared. And thought, why is it sorting it like it's a string still. It's on NUMERIC? Duh, because it is a string, regardless. Convert your data to a Number. I have cs5 and can't save that far back but here's what I wrote:

    PHP Code:
    completeList = new XML();
    completeList.ignoreWhite true;
    completeList.onLoad = function(success) {
        if (
    success) {
            
    tcRecordings.removeAll();
            
    mainheadings this.firstChild.childNodes;
            
            var 
    dataTag:Array = [];
            for (var 
    0a<mainheadings.lengtha++) {
                
    newSub this.firstChild.childNodes[a].childNodes;
                for (var 
    0i<newSub.lengthi++) {
                    
    dataTag.push({ label:newSub[i].attributes.mydata ,data:Number(newSub[i].attributes.mydata) });
                }

            }
            
    dataTag.sortOn("data", Array.DESCENDING | Array.NUMERIC);
            
    tcRecordings.dataProvider dataTag;
        }
    };

    /////-------------------------------------------------------
    AllBtn.onRelease = function() {
        
    this._parent.completeList.load("TCRecordings.xml");
    }; 
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

  11. #11
    Senior Member
    Join Date
    Sep 2007
    Location
    UK
    Posts
    194

    resolved

    mneil - you're a genius !

    Many thanks, have a virtual pint on me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center