Created
December 18, 2014 06:04
-
-
Save bakkot/dfc825a273bfe2ad976b to your computer and use it in GitHub Desktop.
SSC sort by number of children (un-minified)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countChildren(liCommentEle) { | |
return liCommentEle.querySelectorAll('li.comment').length; | |
} | |
function sortList(listEle) { | |
var children = []; | |
for(var i=0; i<listEle.children.length; ++i) { | |
children.push({'ele': listEle.children[i], 'childCount': countChildren(listEle.children[i])}); | |
} | |
var sorted = children.sort(function(a, b){return a.childCount - b.childCount;}); | |
for(var i=sorted.length-1; i>0; --i) { | |
listEle.insertBefore(sorted[i].ele, sorted[0].ele); | |
} | |
} | |
sortList(document.querySelector('ol.commentlist')); | |
var childrenLists = document.querySelectorAll('ul.children'); | |
for(var i=0; i<childrenLists.length; ++i) { | |
sortList(childrenLists[i]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment