code_junkie

What is the easiest way to order a <UL>/<OL> in jQuery?

Nov 12th, 2011
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. <ul class="alphaList">
  2. <li>apples</li>
  3. <li>cats</li>
  4. <li>bears</li>
  5. </ul>
  6.  
  7. var items = $('.alphaList li').get();
  8. items.sort(function(a,b){
  9. var keyA = $(a).text();
  10. var keyB = $(b).text();
  11.  
  12. if (keyA < keyB) return -1;
  13. if (keyA > keyB) return 1;
  14. return 0;
  15. });
  16. var ul = $('.alphaList');
  17. $.each(items, function(i, li){
  18. ul.append(li);
  19. });
Add Comment
Please, Sign In to add comment