Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sortUnorderedList(ul, sortDescending) {
- if(typeof ul == "string")
- ul = document.getElementById(ul);
- // Idiot-proof, remove if you want
- if(!ul) {
- alert("The UL object is null!");
- return;
- }
- // Get the list items and setup an array for sorting
- var lis = ul.getElementsByTagName("LI");
- var vals = [];
- // Populate the array
- for(var i = 0, l = lis.length; i < l; i++)
- vals.push(lis[i].innerHTML);
- // Sort it
- vals.sort();
- // Sometimes you gotta DESC
- if(sortDescending)
- vals.reverse();
- // Change the list on the page
- for(var i = 0, l = lis.length; i < l; i++)
- lis[i].innerHTML = vals[i];
- }
- sortUnorderedList("ID_OF_LIST");
- var mylist = $('#myUL');
- var listitems = mylist.children('li').get();
- listitems.sort(function(a, b) {
- var compA = $(a).text().toUpperCase();
- var compB = $(b).text().toUpperCase();
- return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
- })
- $.each(listitems, function(idx, itm) { mylist.append(itm); });
Add Comment
Please, Sign In to add comment