In Actionscript 3, is there a way to sort the nodes (instances of type XML) in an XMLList in document order (as defined in the XDM spec; “Informally, document order is the order in which nodes appear in the XML serialization of a document”)? Alternately, is there a way to compare the document position of two nodes?
Here’s a little example of what I mean. In the real case, the list is constructed by a much more complicated process and might have hundreds of nodes.
var x:XML = <a><b/><b/></a>;
var b0:XML = x.b[0];
var b1:XML = x.b[1];
var l:XMLList = new XMLList();
l += b1;
l += b0;
var sl:XMLList = documentSortFunction(l);
assertTrue(sl[0] === b0);
I’m not sure I have a lot of hope here, since it seems that ECMA-357 (E4X) doesn’t really have a concept of document, much less document order.
Well, here’s a sketch of the answer I came up with. This hasn’t been tested much yet, but I thought I’d record it before I forget to.
The idea is to implement a function to generate a string for a node, such that the order of the strings is the same as the order of the nodes.