How to sort divs by 2 data attributes?

后端 未结 3 1227
孤城傲影
孤城傲影 2021-01-21 06:42

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.

相关标签:
3条回答
  • 2021-01-21 06:47

    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);
    
    });
    <div id="mydivs">
      
        <div class="sortme" data-status="2" data-order="2">4</div>
        <div class="sortme" data-status="2" data-order="4">3</div>
        <div class="sortme" data-status="1" data-order="2">2</div>
        <div class="sortme" data-status="1" data-order="1">1</div>
        
    
    </div>
    
    <span class="btn btn-primary" id="sortthem" >Sort them</span>
    
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

    0 讨论(0)
  • 2021-01-21 06:58

    You can try this for multiple parameters

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

    0 讨论(0)
  • 2021-01-21 07:07

    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');
    });
    <div id="mydivs">
      <div class="sortme" data-status="2" data-order="2">4</div>
      <div class="sortme" data-status="2" data-order="4">3</div>
      <div class="sortme" data-status="1" data-order="2">2</div>
      <div class="sortme" data-status="1" data-order="1">1</div>
    </div>
    <span class="btn btn-primary" id="sortthem">Sort them</span>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

    0 讨论(0)
提交回复
热议问题