Created
April 6, 2015 08:28
-
-
Save iksi/11594b77ad49da45c96c to your computer and use it in GitHub Desktop.
Starting point for a simple script to sort a list
This file contains hidden or 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
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