Skip to content Skip to sidebar Skip to footer

How To Sort Divs By 2 Data Attributes?

How do I modify my code so that it sorts by both data-status and then data-order? ie the desired result is 1,2,3,4 I need to support IE.

Solution 1:

You can try this for multiple parameters

$(document.body).on('click', "#sortthem", function(){
    var divList = $(".sortme");
    divList.sort(multiSort(["status","order"]));
    $("#mydivs").html(divList);
});
functionmultiSort(fields) {
    returnfunction (a, b) {
        return fields
            .map(function (o) {
               return $(a).data(o) > $(b).data(o) ?  1 :  $(a).data(o) < $(b).data(o) ? -1 : 0 ;
            }).reduce(functionfirstNonZeroValue (el,n) { return el ? el : n }, 0);
    };
}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"rel="stylesheet"/><divid="mydivs"><divclass="sortme"data-status="2"data-order="2">4</div><divclass="sortme"data-status="2"data-order="4">3</div><divclass="sortme"data-status="1"data-order="2">2</div><divclass="sortme"data-status="1"data-order="1">1</div></div><spanclass="btn btn-primary"id="sortthem" >Sort them</span>

Solution 2:

You can add any additional criteria in to the sort directly by checking if the fist criteria result == 0 - then check the second, etc

Updated snippet:

$(document.body).on('click', "#sortthem", function(){

    var divList = $(".sortme");
    
    divList.sort(function(a, b){
    
        var sort1 = $(a).data("status")-$(b).data("status");
        if (sort1 !== 0) return sort1;
        
        var sort2 = $(a).data("order")-$(b).data("order")
        return sort2;        
    });
    
    $("#mydivs").html(divList);

});
<divid="mydivs"><divclass="sortme"data-status="2"data-order="2">4</div><divclass="sortme"data-status="2"data-order="4">3</div><divclass="sortme"data-status="1"data-order="2">2</div><divclass="sortme"data-status="1"data-order="1">1</div></div><spanclass="btn btn-primary"id="sortthem" >Sort them</span><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"rel="stylesheet"/>

Solution 3:

To sort by multiple attributes you simply need to put the secondary sorting logic in place where the two primary attributes match. This can be done in a ternary, as in the following example.

Also note that given your HTML the output will be 1,2,4,3 as the order of the 4 element is lower than that of the 3.

$(document.body).on('click', "#sortthem", function() {
  $(".sortme").sort(function(a, b) {
    let $a = $(a), $b = $(b);
    let aStatus = $a.data('status'), bStatus = $b.data('status');
    let aOrder = $a.data('order'), bOrder = $b.data('order');
        
    return aStatus < bStatus ? -1 : 
      aStatus > bStatus ? 1 : 
      aOrder < bOrder ? -1 :
      aOrder > bOrder ? 1 : 0;
  }).appendTo('#mydivs');
});
<divid="mydivs"><divclass="sortme"data-status="2"data-order="2">4</div><divclass="sortme"data-status="2"data-order="4">3</div><divclass="sortme"data-status="1"data-order="2">2</div><divclass="sortme"data-status="1"data-order="1">1</div></div><spanclass="btn btn-primary"id="sortthem">Sort them</span><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"rel="stylesheet" />

Post a Comment for "How To Sort Divs By 2 Data Attributes?"