Sort elements alphabetically
Sorts HTML elements alphabetically via an array using jQuery
by timmah
HTML
<div id="orig">
<!-- Paste your code to be sorted here -->
</div>
<div id="output">
<!-- Results appear here -->
</div>
JavaScript
var $list = [];
// 'p' is the child element being targeted in #orig - change this to whatever you want to sort
$('p').each(function() {
$list.push($(this));
});
$list.sort(function(a, b){
return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
});
for (var i=0; i<$list.length; i++) {
$('#output').append($list[i]);
}
$('#orig').remove();