javascript sort of HTML elements

后端 未结 5 1321
暗喜
暗喜 2020-11-30 05:56

I\'m trying to sort an li elements and get an unexpacted result I need to sort it three times to get it correctly,

where have I mistaken? javascript

相关标签:
5条回答
  • 2020-11-30 06:23

    There are much better ways to sort.

    1. You need a comparison function that returns the right values: -1, 0, and 1.
    2. localeCompare() is such a comparison function.
    3. You can just move the DOM elements around rather than regenerating the HTML.
    4. You can get the LI elements directly in the original selector.
    5. "#table1" is a more efficient selector than "ol#table1".

    I would suggest this:

    $("div#btn").click(function() {
        var sort_by_name = function(a, b) {
            return a.innerHTML.toLowerCase().localeCompare(b.innerHTML.toLowerCase());
        }
    
        var list = $("#table1 > li").get();
        list.sort(sort_by_name);
        for (var i = 0; i < list.length; i++) {
            list[i].parentNode.appendChild(list[i]);
        }
    });​
    

    Which you can see work here: http://jsfiddle.net/jfriend00/yqd3w/

    0 讨论(0)
  • 2020-11-30 06:23
    var sort_by_name = function(a, b) {
        return $(a).text().toLowerCase().localeCompare($(b).text().toLowerCase());
    }
    

    You can use localeCompare instead to make things more elegant and workable.

    See jsfiddle http://jsfiddle.net/Qww87/11/

    0 讨论(0)
  • 2020-11-30 06:36

    according to documentation it needs numeric values. more intelligible:

        var sort_by_name = function(a, b) {
             if(a.innerHTML.toLowerCase() < b.innerHTML.toLowerCase()) return -1;
             if(a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase()) return 1;
             return 0;
        }
    
    0 讨论(0)
  • 2020-11-30 06:41

    A custom compareFunction needs to return -1, 0 or 1.

    function sort_by_name(a, b) {
        var sa = a.innerHTML.toLowerCase(), sb = b.innerHTML.toLowerCase();
        return sb<sa ? -1 : sb>sa ? 1 : 0;
    }
    
    0 讨论(0)
  • 2020-11-30 06:47

    Use my jquery plugin Sort Content :

       $('ol#table1').sortContent({asc:true})
    

    That's it!

    DEMO : http://jsfiddle.net/abdennour/SjWXU/

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