Skip to main content
Kopetkai
New Participant
March 29, 2009
Answered

Sort Xml list

  • March 29, 2009
  • 3 replies
  • 10144 views
Hello,
I am trying to learn actionscript 3 for work and have a very specific project I need it for. I have been searching every available tutorial have bought a few books to read but I cannot find out how to do a few simple things.
The main thing I need to do right now is sort an xml list by the date of an event before I start picking which nodes to display. (<dt_event_start>) I need the function to be able to sort by date or by alpha for a different xml list I will be sorting later, not both at the same time, just one or the other. My code is sparse right now, all it does is pick events in a certain city and write them all in one big list to a text box on screen. Later I will have to separate out each part to make a header for each event and choose which nodes to display, so i need to sort the xml list before I start displaying the nodes in separate text boxes within a larger container repeated with a loop through the list.
included is one xml entry and the as3 code i have so far
Any help would be greatly appreciated and then I can go looking for the other code I need.
This topic has been closed for replies.
Correct answer
Ok, I've been messing with the code and I got the version you put up to run, but it came back with an error saying ArgumentError: Error #1063: Argument count mismatch on currentevents_fla::MainTimeline/sortXMLList(). Expected 2, got 1.
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

and then I tried to get it to work with my external xml file and got this error and nothing displayed in the output window:
ArgumentError: Error #1063: Argument count mismatch on currenteventsdisplays_fla::MainTimeline/sortXMLList(). Expected 2, got 1.
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
enclosed is my current version of the code trying to load the external xml with the same structure as before

You can't use sortXMLList as an event handler -- the function signature doesn't match. Try it like this:

3 replies

March 30, 2009
You're welcome
March 29, 2009
You'll need to populate an array with the XMLList nodes, sort the array, and then toss them back into an XMLList. Something like this:
Kopetkai
KopetkaiAuthor
New Participant
March 30, 2009
Wow, thanks for the fast replies, I'll work on these and then post my results.
May 9, 2009

This thread looks like the same thing I am looking for but I don't see any of your examples. Is there a reason they are not visible? I could really use some help on this as well. Thank you in advance.


This thread was "imported" from the old forums -- most, if not all, code seems to have disappeared. But here are the sort functions I posted:

function sortXML(source:XML, elementName:Object, fieldName:Object,

options:Object = null):XML

{

     // list of elements we're going to sort

     var list:XMLList=source.elements("*").(name()==elementName);

    

     // list of elements not included in the sort -

     // we place these back into the source node at the end

     var excludeList:XMLList=source.elements("*").(name()!=elementName);

    

     list= sortXMLList(list,fieldName,options);

     list += excludeList;

    

     source.setChildren(list);

     return source;

}

function sortXMLList(list:XMLList, fieldName:Object, options:Object =

null):XMLList

{

     var arr:Array = new Array();

     var ch:XML;

     for each(ch in list)

     {

           arr.push(ch);

     }

     var resultArr:Array = fieldName==null ?

           options==null ? arr.sort() :arr.sort(options)

           : arr.sortOn(fieldName, options);

    

     var result:XMLList = new XMLList();

     for(var i:int=0; i<resultArr.length; i++)

     {

           result += resultArr;

     }

     return result;

}

Inspiring
March 29, 2009
Look at this thread:

http://board.flashkit.com/board/showthread.php?t=777257

It has all the info you need for sorting.