首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >按字母顺序排序项目

按字母顺序排序项目
EN

Stack Overflow用户
提问于 2012-06-21 01:23:52
回答 3查看 854关注 0票数 1

我重新发布这个问题是因为我在第一次发布它时犯了一个错误:我在提问之前没有努力尝试。我想向社区道歉,让他们为我做我的工作

我正在尝试按字母顺序对div中的无序列表进行排序。不是列表项在无序列表中,而是无序列表本身。

这是HTML的外观:

代码语言:javascript
代码运行次数:0
运行
复制
<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>

我希望它看起来像这样:

代码语言:javascript
代码运行次数:0
运行
复制
<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>

这就是我到目前为止所做的:

代码语言:javascript
代码运行次数:0
运行
复制
    <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>
EN

回答 3

Stack Overflow用户

发布于 2012-06-21 01:35:22

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

  1. 构建要排序的节点数组,并附带要排序的节点部分(如果不是易于访问的property).
  2. Sort数组。
  3. 根据新顺序重新构建DOM树。

在这种情况下,您想要排序的节点通过#FindByCategory > ul进行匹配,并且您可以有效地按其文本内容进行排序。所以:

代码语言:javascript
代码运行次数:0
运行
复制
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
}
票数 1
EN

Stack Overflow用户

发布于 2012-06-21 01:50:19

尝尝这个

代码语言:javascript
代码运行次数:0
运行
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2012-06-21 01:26:42

这是一个用于jQuery的瑞士军刀排序插件的链接。http://tinysort.sjeiti.com/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11124874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档