View Full Version : Sorting an XMLListCollection

04-12-2006, 08:40 AM
Hi All...

I was wondering if anyone has had occasion to sort an XMLListCollection, or if anyone knows of an example of how to do this? I'm confused about how to use the Sort class with this Collection...

04-12-2006, 09:58 AM
XMLListCollection extends ListCollectionView and ListCollectionView has a sort() method. have you had a look at that?

04-12-2006, 07:19 PM
Yeah, I spent a few hours looking at it. You create a Sort object and a SortField object and so on. The trick is figuring out what the sort field is.

All the examples in the docs are based upon an ArrayListCollection and key:value pairs. You build up a list of key:value pairs and then give the SortField the name of the key for the value you want your collection sorted on and call refresh and it does it. Easy enough.

XMLListCollection is a bit more complex. I spent quite a lot of time trying to figure out how to represent the field I wanted it sorted on. I need to sort on an attribute of an XML node. I tried every variation of E4X syntax I could think of, various arrangements of the nodes in the XMLList object I handed to the XMLListCollection and so on. Nothing worked. Everything I tried resulted in confusing the XMLListCollection to the point where the view gave nothing at all.

Without the docs explaining it, or the source for the collection itself or an example, there are too many different ways to do this to stumble upon the correct way by trial and error. So I was hoping someone knew of an example.

04-12-2006, 07:30 PM
i'll sort one out for you in the next day or so

04-12-2006, 10:46 PM
i'll sort one out for you in the next day or so


11-18-2006, 09:13 PM
I too have the exact same need. Does anyone have some simple code example of how to sort and XMLList?

11-20-2006, 08:06 AM
Here's some quickie code that might help give you an idea bladn

public function sortData(value : XMLListCollection) : void
// Create the Sort instance.
var sort:Sort = new Sort();

// Set the sort field; sort on the last name first, first name second.
// Both fields are case-insensitive.
sort.fields = [new SortField("field1", true)];

// Assign the Sort object to the view.
value.sort = sort;

// Apply the sort to the collection.