Skip to content Skip to sidebar Skip to footer

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)
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="testWrapper"><divclass="test"data-x="false"data-y="7"data-z="20">a</div><divclass="test"data-x="true"data-y="3"data-z="25">b</div><divclass="test"data-x="false"data-y="7"data-z="25">c</div><divclass="test"data-x="true"data-y="5"data-z="20">d</div></div>

Post a Comment for "Sort Ul By Multiple Values"