How may I sort a list alphabetically using jQuery?

后端 未结 10 2317
灰色年华
灰色年华 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:43

    I was looking to do this myself, and I wasnt satisfied with any of the answers provided simply because, I believe, they are quadratic time, and I need to do this on lists hundreds of items long.

    I ended up extending jquery, and my solution uses jquery, but could easily be modified to use straight javascript.

    I only access each item twice, and perform one linearithmic sort, so this should, I think, work out to be a lot faster on large datasets, though I freely confess I could be mistaken there:

    sortList: function() {
       if (!this.is("ul") || !this.length)
          return
       else {
          var getData = function(ul) {
             var lis     = ul.find('li'),
                 liData  = {
                   liTexts : []
                }; 
    
             for(var i = 0; i" + obj[sortedTexts[i]].html+"
  • "; } return htmlStr; }; this.html(processData(getData(this))); } }

提交回复
热议问题