/*------------------------------------------------------------------------------
sort()
------------------------------------------------------------------------------*/
function sort(xpathParent, xpathChildren, fnCompare)
{
// Retrieve the parent node; all the children will be sorted.
var xmlOrigParent = getNode(xpathParent)
var xmlItems = xmlOrigParent.selectNodes(xpathChildren);
if (1 < count(xmlItems))
{
// Store the (pointers to) items in an array for faster access.
rgItems = new Array();
while (xmlItem = xmlItems.nextNode())
rgItems.push(xmlItem);
// Sort the array (the XML does not change).
rgItems.sort(fnCompare);
// Now that the array is sorted the DOM should be updated.
var xmlSortParent = xmlOrigParent.cloneNode(true);
var xmlClones = xmlSortParent.selectNodes(xpathChildren);
xmlClones.removeAll();
// Update the nodelist, each item is moved only once.
// (each change is causing several notifications to be fired.)
for (var i=0; i<rgItems.length; i++)
xmlSortParent.appendChild(rgItems[i].cloneNode(true));
xmlOrigParent.parentNode.replaceChild(xmlSortParent, xmlOrigParent);
}
}