Sorting nested lists

后端 未结 1 1725
暖寄归人
暖寄归人 2021-01-25 10:26

Given is a nested list, with the following markup (and sadly can\'t be changed at this moment). I want to sort this list (and all nested lists by the \'a\'-tag title.) The firs

相关标签:
1条回答
  • 2021-01-25 11:05

    Sorting everything is a matter of appending all nodes to their respective parents in alphabetical order.

    function uCase(elem) {
        return $.trim( $(elem).text().toUpperCase() )
    }
    function compareFirstLink(a, b) {
        var A = uCase( $(a).first('a') ),
            B = uCase( $(b).first('a') );       
        return (A > B) ? 1 : -1;
    }
    $(function () {
        var $sortables = $("ul:has(div:first-child), li:not(.fixedOrder)");
        $sortables.sort(compareFirstLink).each(function () {
            this.parentNode.appendChild(this);
        });
    });
    
    0 讨论(0)
提交回复
热议问题