JavaScript node sorting 2_javascript tips

Source: Internet
Author: User
Tags uuid
Copy Code code as follows:

Inspiration from
Http://www.cnblogs.com/jkisjk/archive/2011/01/28/array_quickly_sortby.html
var hasduplicate = false;
var sortby = function (nodes) {
var result = [], array = [], n = nodes.length, i = n, node;
while (node = nodes[--n]) {
(Array[n] = new Number (~~node.sourceindex)). _ = node;
}
Array.Sort (function (a,b) {
if (A = = = b) Hasduplicate = true;
return a-b;
});
while (i)
Result[--i] = array[i]._;
return result;
}

But the standard browser does not support this attribute, in IE, the XML document also does not have this attribute, then needs to follow the node parentnode and nextsibling, but if is only 22 comparison, the speed is cannot ascend. So we're going to compare the order of the children of the recent public ancestors. At this point, the power of the algorithm is reflected. This is the first edition, according to a friend provided by the LCA out of things, of course, the general idea is attributed to the JK great God. But the actual effect is not satisfactory, the sortorder is slower than the jquery, the problem is to ask for LCA.
Copy Code code as follows:

Based on the ideas that JK offers here
http://www.cnblogs.com/rubylouvre/archive/2011/01/28/1947286.html#2020900
var tick = 0, hasduplicate = false;
var Rage = {
Form http://www.cnblogs.com/GrayZhang/archive/2010/12/29/find-closest-common-parent.html
Getlca:function (nodes) {
var hash = {}, i = 0,
attr = "Data-find" + (++tick),
Length = Nodes.length,
Node
Parent
Counter = 0,
Uuid
while (node = nodes[i++]) {
parent = node;
while (parent) {
if (Parent.nodetype = = 1) {
Break
}
UUID = Parent.getattribute (attr);
if (!uuid) {
UUID = "_" + (++counter);
Parent.setattribute (ATTR,UUID);
Hash[uuid] = {node:parent,count:1};
}else{
Hash[uuid].count + +;
}
parent = Parent.parentnode;
}
}
for (var i in hash) {
if (Hash[i].count = = length) {
return hash[i].node;
}
}
},
Getlist:function (nodes,parent) {//Get the current element to all ancestors of the nearest common ancestor, including himself
var list = [];
while (node) {
if (node = = parent) {
Break
}
List.unshift (node);
node = Node.parentnode;
}
return list;
},
Getlists:function () {
var lists = [], getlist = rage.getlist, i=0, node, list;
while (node = nodes[i++]) {
List = GetList (node,parent);
if (list.length) {
lists[lists.length] = list;
}
}
return lists;
},
Sortlist:function (a,b) {
var n = math.min (a.length,b.length), AP,BP;
for (var i=0 i < n; i++) {
AP = A[I],BP = B[i]
if (AP!== BP) {
while (AP = ap.nextsibling) {
if (AP = = BP) {
Return-1
}
}
Return 1
}
}
return a.length-b.length;
},
Uniquesort:function (nodes) {
var length = Nodes.length;
var LCA = Rage.getlca (nodes);
var lists = rage.getlists (NODES,LCA);
Lists.sort (rage.sortlist);
var list, i = 0, result = [];
while (list = lists[i++]) {
Result[result.length] List.pop ();
}
if (result.length!== length) {
Result.unshift (LAC);
if (result.length!= length) {
Hasduplicate = true;
}
}
return result;
}
}

Here is the second edition, improved, and finally three times times faster than the one in jquery (the test object is a document with more than 260 nodes)
Copy Code code as follows:

var hasduplicate = false;
var Rage = {
Getlist:function (node) {
var list = [];
while (node) {
if (Node.nodetype = = 9) {
Break
}
List.unshift (node);
node = Node.parentnode;
}
return list;
},
Getlists:function (nodes) {
var lists = [], getlist = rage.getlist, i=0, node;
while (node = nodes[i++]) {
lists[Lists.length] = getlist (node);
}
return lists;
},
Slicelist:function (lists,num) {
var result = [], i = 0, list;
while (list = lists[i++]) {
List = List.slice (num);
if (list.length) {
result[result.length] = list;
}
}
return result;
},
Sortlist:function (a,b) {
var n = math.min (a.length,b.length), AP,BP;
for (var i=0 i < n; i++) {
AP = A[I],BP = B[i]
if (AP!== BP) {
while (AP = ap.nextsibling) {
if (AP = = BP) {
Return-1
}
}
Return 1
}
}
return a.length-b.length;
},
Uniquesort:function (nodes) {
var length = Nodes.length;
var lists = rage.getlists (nodes);
Lists.sort (function (a,b) {
return a.length-b.length;
});
var depth = lists[0].length, length = lists.length, parent, cut, ii = 0;
for (var i =0 < depth; i++) {
parent = Lists[0][i];
Cut = true;
for (var j = 1;j < length; J + +) {
if (parent!== Lists[j][i]) {
Cut = false;
Break
}
}
if (cut) {
ii++
}else{
Break
}
}
var LCA = lists[0][ii-1];
Lists = Rage.slicelist (LISTS,II);
Lists.sort (rage.sortlist);
var list, i = 0, result = [];
while (list = lists[i++]) {
Result[result.length] = List.pop ();
}
if (result.length!== length) {
Result.unshift (LCA);
if (result.length!= length) {
Hasduplicate = true;
}
}
return result;
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.