Skip to content

Instantly share code, notes, and snippets.

@bakkot
Created December 18, 2014 06:04
Show Gist options
  • Save bakkot/dfc825a273bfe2ad976b to your computer and use it in GitHub Desktop.
Save bakkot/dfc825a273bfe2ad976b to your computer and use it in GitHub Desktop.
SSC sort by number of children (un-minified)
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