Sort Ul By Multiple Values
I have a ul list with li-s.. Each li contains three attributes: attr-x, attr-y and attr-z. I am trying to sort the ul according to: top group is attr-x=true. bottom group is attr-x
Solution 1:
You can use sort()
like this.
var sorted = $('.testWrapper div').sort(function(a, b) {
return ($(b).data('x') - $(a).data('x')) || ($(b).data('y') - $(a).data('y')) || ($(b).data('z') - $(a).data('z'))
})
$(".testWrapper").html(sorted)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="testWrapper">
<div class="test" data-x="false" data-y="7" data-z="20">a</div>
<div class="test" data-x="true" data-y="3" data-z="25">b</div>
<div class="test" data-x="false" data-y="7" data-z="25">c</div>
<div class="test" data-x="true" data-y="5" data-z="20">d</div>
</div>
Post a Comment for "Sort Ul By Multiple Values"