Sorting XMLListCollection by attribute

Just want to share the result of a small puzzle I had about sorting elements of an XMLListCollection by one of the attributes of such an element. At first, I tried sorting the elements of the XMLList I had, but the API does not provide any methods to do that. Bruce Phillips wrote a snippet that pointed me to right direction. So, here it is:

var doc:XML = <root>
<item name=“C”/>
<item name=“B”/>
<item name=“A”/>
</root>
var collection:XMLListCollection = doc.item
var sort:Sort = new Sort()
sort.fields = [ new SortField(“@name”, true) ]
collection.sort = sort
collection.refresh()

As expected, the collection will show:

<item name=“A”/>
<item name=“B”/>
<item name=“C”/>

The XMLHelper class in the Dunelox library has been extended with a sort function to encapsulate this.

comments powered by Disqus