Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

sort list of links

By: a guest on Nov 10th, 2011  |  syntax: JavaScript  |  size: 0.77 KB  |  views: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <script type="text/javascript">
  2.          function sortUnorderedList(ul, sortDescending) {
  3.           if(typeof ul == "string")
  4.                 ul = document.getElementById(ul);
  5.        
  6.           // Get the list items and setup an array for sorting
  7.           var lis = ul.getElementsByTagName("a");
  8.           var vals = [];
  9.        
  10.           // Populate the array
  11.           for(var i = 0, l = lis.length; i < l; i++)
  12.                 vals.push(lis[i].text);
  13.  
  14.   // Sort it
  15.   vals.sort();
  16.  
  17.   // Sometimes you gotta DESC
  18.   if(sortDescending)
  19.     vals.reverse();
  20.  
  21.   // Change the list on the page
  22.   for(var i = 0, l = lis.length; i < l; i++)
  23.     lis[i].innerHTML = vals[i];
  24. }
  25.  
  26.       var desc = false;
  27.  
  28.       function listsort() {
  29.         sortUnorderedList("policy", desc);
  30.         desc = !desc;
  31.       }
  32.  
  33.       window.onload = listsort;
  34.     </script>
  35.