How to sort divs by 2 data attributes?

后端 未结 3 1269
孤城傲影
孤城傲影 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);
    
    });
    4
    3
    2
    1
    Sort them

提交回复
热议问题