How to sort divs by 2 data attributes?

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

提交回复
热议问题