绿豆.Net

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

/*------------------------------------------------------------------------------
 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);
 }
}

posted on 2007-03-26 10:42  杜军  阅读(189)  评论(0)    收藏  举报