22 July 2008

Sort XML by Attribute in Actionscript 3

I love working with E4X, but I wish there was an easy way to sort an XML based on an attribute.

After looking around I couldn't really find a good solution or maybe people weren't explaining themselves correctly, or I wasn't googling correctly. So I decided just to write my own small utility class.

When I thought about it. Arrays provide a superb sortOn() function, and it's relatively easy to leverage that.

code

I created a sortXMLByAttribute() public static function in my XMLUtils Class

public static function sortXMLByAttribute
 	(
		$xml		:	XML,
		$attribute	:	String,
		$options	:	Object	=	null,
		$copy		:	Boolean	=	false
	)
	:XML
 {
	//store in array to sort on
	var xmlArray:Array	= new Array();
	var item:XML;
	for each(item in $xml.children())
	{
		var object:Object = {
			data	: item, 
			order	: item.attribute($attribute)
		};
		xmlArray.push(object);
	}

	//sort using the power of Array.sortOn()
	xmlArray.sortOn('order',$options);

	//create a new XMLList with sorted XML
	var sortedXmlList:XMLList = new XMLList();
	var xmlObject:Object;
	for each(xmlObject in xmlArray )
	{
		sortedXmlList += xmlObject.data;
	}
	
	if($copy)
	{
		//don't modify original
		return	$xml.copy().setChildren(sortedXmlList);
	}
	else
	{
		//original modified
		return $xml.setChildren(sortedXmlList);
	}
 }

say what?

how to use

$xml : The XML that needs to be sorted
$attribute : The attribute string to sort on
$options : The sorting options, see sortOn()
$copy : If false, original XML is modified.

Here is an example, note this is in a package called XMLUtils

var xml:XML =
<body id="someId">
        <p displayOrder="15">Hello</p>
        <p displayOrder="7">World</p>
        <p displayOrder="3">Is</p>
        <p displayOrder="9">This</p>
        <p displayOrder="25">Thing</p>
        <p displayOrder="13">Working</p>
</body>

// original XML object
trace("BEFORE:" + xml);

XMLUtils.sortXMLByAttribute(
	xml,
	'displayOrder'
);
trace("After:" + xml);

XMLUtils.sortXMLByAttribute(
	xml,
	'displayOrder', 
	Array.NUMERIC
);
trace("Array.NUMERIC:" + xml");

//multiple options not don't modify orginal
var reverseXML:XML = XMLUtils.sortXMLByAttribute(
	xml,
	'displayOrder', 
	Array.NUMERIC | Array.DESCENDING, 
	true
);
trace("Array.NUMERIC | Array.DESCENDING:" + reverseXML);

source

Source code (in package+ + example) xmlutil.zip

 

comment

what they saidwho said it

Nice utility,

Thank You

2008-07-25
Chetan Sachdev

Thanks! Saves me from having to write one.

2008-08-14
Justin_P

Cool stuff !

2008-09-01
MajorDOOM

That’s how it should be. Thanks a lot.

2008-09-29
fuX

Thanks!!!!!!!!!!!!!!!!!!!

2008-11-20
Monica H

Perfect… You is a genius! Thanks!

2008-11-28
Jsr

It Works effortlessly.. Great job.

2009-11-30
dineshviswanath


note: you can only submit after you hit preview


nuff-respec is a weblog written by daniel bulli a senior web programmer in boston, ma.
more >

contact | resume | profile | twitter

recently :::

diversions :::

45+ Amazing Insect Shots in Photography
Insects are one of the most fascinating creatures on earth. There are more than 800, 000 species of insects in the world.
Grayscale color | Stroep Blog
This is how I create a grayscale color in actionscript 3.
Google Flash API
This is great ... google has made this easy ... stay tuned to see what i am working on ...
25 Free Mac Apps That Will Boost Your Productivity
There are many applications that can help you work faster and efficiently. Though, not many applications come cheap.
you still want more »