Posted By

denisediniz on 07/16/13


Tagged

sort list jquery alphabetically


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Priestd09


jQuery to sort alphabetically a list with anchors


 / Published in: jQuery
 

URL: http://stackoverflow.com/questions/13394796/javascript-jquery-to-sort-alphabetically-a-list-with-anchors

jQuery to sort alphabetically a list with anchors

  1. $(function() {
  2. $.fn.sortList = function() {
  3. var mylist = $(this);
  4. var listitems = $('li', mylist).get();
  5. listitems.sort(function(a, b) {
  6. var compA = $(a).text().toUpperCase();
  7. var compB = $(b).text().toUpperCase();
  8. return (compA < compB) ? -1 : 1;
  9. });
  10. $.each(listitems, function(i, itm) {
  11. mylist.append(itm);
  12. });
  13. }
  14.  
  15. $("ul#demoOne").sortList();
  16.  
  17. });

Report this snippet  

You need to login to post a comment.