Skip to content

Instantly share code, notes, and snippets.

Created Jan 28, 2014
What would you like to do?
jQuery: Sort a list alphabetically
$(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) {
Copy link

metalevel-tech commented Mar 22, 2020

Works great, thanks! One modification that I made in my case, where I'm having few separate lists identified by a same class, was:

$('ul.demoOne').each(function(){ $(this).sortList; });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment