Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vyspiansky/8870467 to your computer and use it in GitHub Desktop.
Save vyspiansky/8870467 to your computer and use it in GitHub Desktop.
jQuery: sort alphabetically a list with anchors
/*
* Source: http://goo.gl/bi40M3
*/
$(function() {
$.fn.sortList = function() {
var mylist = $(this);
var listitems = $('li', mylist).get();
listitems.sort(function(a, b) {
var compA = $(a).text().toUpperCase();
var compB = $(b).text().toUpperCase();
return (compA < compB) ? -1 : 1;
});
$.each(listitems, function(i, itm) {
mylist.append(itm);
});
}
// Usage:
$("ul#demoOne").sortList();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment