Alphabetize a list using JS or jQuery

后端 未结 5 991
孤独总比滥情好
孤独总比滥情好 2020-12-18 16:32

I\'m trying to take the contents of a list, that is not in alphabetical order, then by adding each item to an array, sort them into alphabetical order and insert them back i

5条回答
  •  北海茫月
    2020-12-18 17:04

    Adjust id to className at .alphabet element to prevent duplicate ids in document.

    You can use String.prototype.split() with "" as parameter, Array.prototype.sort(), .text()

    var li = $(".alphabet"), text = li.text().split("").sort();
    
    li.text(function(index) {
      return text[index]
    })
    
    
    • B
    • C
    • A

提交回复
热议问题