PDA

View Full Version : Parsing XML: simple way to sort by node?



redconservatory
September 26th, 2009, 09:25 PM
I have the following function, and so far it works great:


private function parseTimeline(myXML:XML):void {

var itemList:XMLList = myXML.item;
for each (var item:XML in itemList) {

// trace(item.photo);
dispatchEvent(new ModelEvent_Setup(ModelEvent_Setup.SETUP, true, false, item.photo, item.title,item.text, item.year, item.month));
}

}

The dispatchEvent is used to dispatch the xml information to another class, where I will use the information to set up my graphics.

However, before the dispatching of the Event, I would really like to sort my information by the "year" node of my xml file (the "item.year"). I've seen examples of how to sort by the xml attribute online, but not the node. Is there any simple way to do this?

wvxvw
September 27th, 2009, 03:46 AM
var xml:XML =
<foo>
<year>1978</year>
<year>2009</year>
<year>2048</year>
<year>1812</year>
</foo>;

var a:Array = [];
xml.*.(a.push(valueOf()));

a.sortOn("year");

var list:XMLList = new XMLList();
while (a.length)
{
list += a.pop();
}

xml.setChildren(list);

trace(xml.toXMLString());
See if that's what you wanted

redconservatory
September 27th, 2009, 05:24 PM
That works great, thanks!