I'd like list elements with class 'active' to be ordered alphabetically at the top of a list, with the remainder of the list underneath ordered separately. In other words,
<li><label><span>zz</span></label></li>
<li class='active'><label><span>rr</span></label></li>
<li class='active'><label><span>bb</span></label></li>
<li><label><span>hh</span></label></li>
<li><label><span>ii</span></label></li>
<li class='active'><label><span>yy</span></label></li>
<li><label><span>kk</span></label></li>
<li><label><span>mm</span></label></li>
results in..
bb
rr
yy
hh
ii
kk
mm
zz
I think I've got the right approach but I can't seem to get this to work. Something to do with the append I think. Can anyone help me get over the last hurdle? https://jsfiddle.net/Lcz47b9y/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="rootUl">
<li><label><span>zz</span></label></li>
<li class='active'><label><span>rr</span></label></li>
<li class='active'><label><span>bb</span></label></li>
<li><label><span>hh</span></label></li>
<li><label><span>ii</span></label></li>
<li class='active'><label><span>yy</span></label></li>
<li><label><span>kk</span></label></li>
<li><label><span>mm</span></label></li>
</ul>
.
var myli = $('#rootUl li.active label').children('span').detach().sort(sortByText);
$('#rootUl').append($(myli))
function sortByText(a, b) {
var first = $.trim($(a).text());
var second = $.trim($(b).text());
return first.localeCompare(second);
}