Jquery sort list alphabetically


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var mylist = $('ul');
  2. var listitems = mylist.children('li').get();
  3. listitems.sort(function(a, b) {
  4. var compA = $(a).text().toUpperCase();
  5. var compB = $(b).text().toUpperCase();
  6. return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
  7. })
  8. $.each(listitems, function(idx, itm) { mylist.append(itm); });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.