Skip to content

Instantly share code, notes, and snippets.

@oriolrivera
Last active December 20, 2015 02:19
Show Gist options
  • Save oriolrivera/6055235 to your computer and use it in GitHub Desktop.
Save oriolrivera/6055235 to your computer and use it in GitHub Desktop.
Ordenar alfabéticamente elementos del html con jquery
<!doctype html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Ordenar alfabéticamente elementos del html con jquery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(function(){
var mylist = $('#myId');
var listitems = mylist.children('li').get();
listitems.sort(function(a, b) {
var compA = $(a).text().toUpperCase();
var compB = $(b).text().toUpperCase();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
})
$.each(listitems, function(idx, itm) { mylist.append(itm); });
});
</script>
</head>
<h1>ordenar alfabéticamente elementos del html con jquery</h1>
<ul id="myId">
<li>b</li>
<li>a</li>
<li>c</li>
<li>m</li>
<li>a</li>
</ul>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment