Javascript node sorting 2

Source: Internet
Author: User

Copy codeThe Code is as follows: // inspired
// 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;
}

However, standard browsers do not support this attribute. XML documents do not have this attribute in IE. In this case, parentNode and nextSibling of the data node must be compared with parentNode and nextSibling, the speed cannot be improved. Therefore, we will compare the sequence of the children of the recent common ancestor. Then, the power of the algorithm is reflected. This is the first version. According to the LCA provided by a friend, the general idea is still attributed to the great god JK. However, the actual effect is unsatisfactory, Which is slower than the jQuery sortOrder. It is estimated that the problem lies in the evaluation of LCA.Copy codeThe Code is as follows: // based on the ideas provided by JK
// 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) {// obtain all the ancestors of the current element to the nearest common ancestor, including
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;
}
}

The following is the second version. After improvement, it is three times faster than jQuery (the test object is a document with more than 260 nodes)Copy codeThe Code is 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; I <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.