How may I sort a list alphabetically using jQuery?

后端 未结 10 2128
灰色年华
灰色年华 2020-11-21 23:13

I\'m a bit out of my depth here and I\'m hoping this is actually possible.

I\'d like to be able to call a function that would sort all the items in my list alphabeti

10条回答
  •  温柔的废话
    2020-11-21 23:41

    $(".list li").sort(asc_sort).appendTo('.list');
    //$("#debug").text("Output:");
    // accending sort
    function asc_sort(a, b){
        return ($(b).text()) < ($(a).text()) ? 1 : -1;    
    }
    
    // decending sort
    function dec_sort(a, b){
        return ($(b).text()) > ($(a).text()) ? 1 : -1;    
    }
    

    live demo : http://jsbin.com/eculis/876/edit

提交回复
热议问题