Skip to content

Instantly share code, notes, and snippets.

@iksi
Created April 6, 2015 08:28
Show Gist options
  • Save iksi/11594b77ad49da45c96c to your computer and use it in GitHub Desktop.
Save iksi/11594b77ad49da45c96c to your computer and use it in GitHub Desktop.
Starting point for a simple script to sort a list
var list = document.getElementById('mylist');
var items = list.childNodes;
var itemsArr = [];
for (var i in items) {
if (items[i].nodeType == 1) { // get rid of the whitespace text nodes
itemsArr.push(items[i]);
}
}
itemsArr.sort(function(a, b) {
return a.innerHTML == b.innerHTML
? 0
: (a.innerHTML > b.innerHTML ? 1 : -1);
});
for (i = 0; i < itemsArr.length; ++i) {
list.appendChild(itemsArr[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment