Javascript 按字母顺序排列项目

Javascript 按字母顺序排列项目,javascript,jquery,javascript-events,Javascript,Jquery,Javascript Events,我之所以重新发帖,是因为我第一次发帖时犯了一个错误:我在发帖前没有尽全力。我想向社区道歉,因为我要求它为我工作 我试图在div中按字母顺序排列无序列表。不是列表项在无序列表中,而是无序列表本身 这是HTML的外观: <div class="FindByCategory"> <ul> <li> B. This is the first list item in the second unordered list</li>

我之所以重新发帖,是因为我第一次发帖时犯了一个错误:我在发帖前没有尽全力。我想向社区道歉,因为我要求它为我工作

我试图在div中按字母顺序排列无序列表。不是列表项在无序列表中,而是无序列表本身

这是HTML的外观:

<div class="FindByCategory">
    <ul>
         <li> B. This is the first list item in the second unordered list</li>
         <li> B. This is the second list item in the second unordered list</li>
    </ul>
    <ul>
         <li> A. This is the first list item in the first unordered list</li>
         <li> A. This is the second list item in the first unordered list</li>
    </ul>
</div>

  • B.这是第二个无序列表中的第一个列表项
  • B.这是第二个无序列表中的第二个列表项
  • A.这是第一个无序列表中的第一个列表项
  • A.这是第一个无序列表中的第二个列表项
我希望它看起来像这样:

<div class="FindByCategory">
    <ul>
         <li> A. This is the first list item in the first unordered list</li>
         <li> A. This is the second list item in the first unordered list</li>
    </ul>
    <ul>
         <li> B. This is the first list item in the second unordered list</li>
         <li> B. This is the second list item in the second unordered list</li>
    </ul>
</div>

  • A.这是第一个无序列表中的第一个列表项
  • A.这是第一个无序列表中的第二个列表项
  • B.这是第二个无序列表中的第一个列表项
  • B.这是第二个无序列表中的第二个列表项
这就是我到目前为止所做的:

    <script>
        function sortUnorderedList(div.FindByCategory, sortDescending) {
  if(typeof div.FindByCategory == "string")
    div = document.getElementById(div.FindByCategory);
  var uls = ul.getElementsByTagName("UL");
  var vals = [];
  for(var i = 0, l = uls.length; i < l; i++)
    vals.push(uls[i].innerHTML);
  vals.sort();
  for(var i = 0, l = uls.length; i < l; i++)
    uls[i].innerHTML = vals[i];
}
$(function(){
    FindByCategory()
    }
    </script>

函数sortUnorderedList(div.FindByCategory,sortDescending){
if(div.FindByCategory的类型==“字符串”)
div=document.getElementById(div.FindByCategory);
var uls=ul.getElementsByTagName(“ul”);
var VAL=[];
对于(变量i=0,l=uls.length;i
这里有一个指向jQuery的瑞士军刀排序插件的链接。

对任何DOM节点排序时,有三个简单的步骤:

  • 构建一个要排序的节点数组,并附上要排序的节点部分(若它不是一个容易访问的属性)
  • 对数组进行排序
  • 基于新的顺序重建DOM树
  • 在这种情况下,您要排序的节点由
    #FindByCategory>ul
    匹配,并且您可以根据它们的文本内容进行有效排序。因此:

    var qsa = document.querySelectorAll("#FindByCateory > ul"), l = qsa.length, i, arr = [];
    for( i=0; i<l; i++) {
        arr[i] = [qsa[i],qsa[i].textContent || qsa[i].innerText];
        // the text content is not readily accessible, since it depends on the browser
    }
    // now sort the array:
    arr.sort(function(a,b) {return a[1] < b[1] ? -1 : (a[1] == b[1] ? 0 : 1);});
    // and now re-append them
    for( i=0; i<l; i++) {
        arr[i][0].parentNode.appendChild(arr[i][0]);
        // by re-appending the nodes, they will emerge sorted
    }
    
    var qsa=document.queryselectoral(“#FindByCateory>ul”),l=qsa.length,i,arr=[];
    对于(i=0;i试试这个

    <script type="text/javascript">
    
                    var array = new Array();
    
                    for(var i = 0 ; i<4 ; i++){
    
                        var string = $("li:eq("+i+")").text();
                        array[i]  = string;
    
                    }
    
                    array.sort();
    
                    alert(array);
    
    
                </script>
    
    
    var数组=新数组();
    对于(var i=0;i