
function sortBkm(root) {

	var returnTo = this.pageNum
		, chd = root.children
		, names = {}
		, key, bkm;

	if(!chd) { return; }

	for(key = -1; bkm = chd[++key];){
		bkm.execute();
		bkm.pageNum = this.pageNum;
		bkm.order = key;
		names[bkm.pageNum] = {};
		bkm.children && sortBkm.call(this, bkm);
	}

	chd.sort(function(a, b){
		var p = a.pageNum - b.pageNum;
		return p ? p : a.order - b.order;
	});

	for(key = -1; bkm = chd[++key];){
		if(names[bkm.pageNum][bkm.name] && !bkm.children){
			bkm.remove();
			continue;
		}
		root.insertChild(bkm, chd.length);
		names[bkm.pageNum][bkm.name] = true;
	}

	this.pageNum = returnTo;

}

var cEnable = "event.rc = (event.target != null);"
	, cExec = "sortBkm.call(this, this.bookmarkRoot);"
	, cLabel = "Ordenar Marcadores";

app.addMenuItem({ 
	cName: "sortBkm", 
	cUser: cLabel,
	cParent: "Tools", 
	cEnable: cEnable,
	cExec: cExec,
	nPos: 0 
});

app.addToolButton({
	cName: "sortBkmButton", 
	cLabel: cLabel,
	cEnable: cEnable,
	cExec: cExec,
	nPos: -1 
});	


@@ -139,34 +139,33 @@
 %7D%0D%0A%0D%0A%09for(key = 
--1
+0
 ; bkm = chd%5B++ke
@@ -152,37 +152,37 @@
  = 0; bkm = chd%5B
-++
 key
+++
 %5D;)%7B%0D%0A%09%09bkm.exec
@@ -435,18 +435,17 @@
 r(key = 
--1
+0
 ; bkm = 
@@ -452,13 +452,13 @@
 chd%5B
-++
 key
+++
 %5D;)%7B
@@ -1063,10 +1063,8 @@
 %0D%0A%7D);%09%0D%0A
-%0D%0A

simplify loop