Sorting XMLListCollection
Posted on Monday, January 29, 2007 @ 18:05 CET
Earlier I was trying to parse some OPML in a Flex2 app, and I've got to say that playing with XML in AS3 is a joy, compared with how it was in earlier versions of ActionScript. I heart the E4X!
In any case, the parsing wasn't the problem. I was passing some data along to a datagrid as a XMLListCollection, and wanted to sort the list alphabetically using one of the attributes once the data was loaded. It took a good deal of Googling and trying things out but in the end I found a method that did the trick, so here it is for future usage:
import mx.collections.XMLListCollection;import mx.collections.Sort;
import mx.collections.SortField;
/**
* @method sortData
* @tooltip Sorts an XMLListCollection and applies it
* @param list The list to sort
* @param field The field to sort by
*/
private function sortData(list:XMLListCollection, field:String):void {
var sort:Sort = new Sort();
// Set the sort field to sort by the text attribute
sort.fields = [new SortField(field, true)];
// Apply the sort to the collection
list.sort = sort;
list.refresh();
}
So to sort a feedList XMLListCollection object by its "text" attribute you say sortData(feedList, "@text");. I would've liked to return the sorted list instead of manipulating it directly, but then you have to remember to call refresh() on it, which applies the sort, so I figured this was easier.
- paulo




Thanks Paulo. you saved my day. I was really very helpful.
# December 09, 2008 07:41 CET
Post a comment:
You must have Flash and JavaScript enabled to post a comment.