I've seen a few examples out there, but not finding a way to sort a list onload.
In this example by Jennifer Perrin, she shows how to sort with two links (sort ascending/sort descending).
Can you offer a tweak to do this on page load instead?
markup
<h1>Sort by:
<a href="#" class="link-sort-list asc">Desc</a>
<a href="#" class="link-sort-list desc">Asc</a>
</h1>
<ul id="sort-list">
<li>Orange</li>
<li>Pear</li>
<li>Bananna</li>
<li>Apple</li>
<li>Cucumber</li>
</ul>
jquery
$(document).ready(function() {
$('.link-sort-list').click(function(e) {
var $sort = this;
var $list = $('#sort-list');
var $listLi = $('li',$list);
$listLi.sort(function(a, b){
var keyA = $(a).text();
var keyB = $(b).text();
if($($sort).hasClass('asc')){
return (keyA > keyB) ? 1 : 0;
} else {
return (keyA < keyB) ? 1 : 0;
}
});
$.each($listLi, function(index, row){
$list.append(row);
});
e.preventDefault();
});
});
would 'win' (override) but apparently not. With the example below, http://stackoverflow.com/a/27446872/2368444 at-least the list gets put into *CLOSE-TO* descending alpha. A real head-scratcher.
– Alan Dec 12 '14 at 16:11