简体   繁体   English

遍历元素对孩子进行排序

[英]Iterate through elements to sort their childrens

I'm trying to iterate through all the elements with a specific class to sort their children based on their value attributes. 我正在尝试遍历具有特定类的所有元素,以根据其值属性对子级进行排序。 I need to sort the children separately for each parent elements. 我需要对每个父元素分别对子元素进行排序。

Html structure : HTML结构:

<ul class="parentClass">
   <li value="1">...</li>
   <li value="10">...</li> 
   <li value="8">...</li> 
</ul>
<ul class="parentClass">
   <li value="8">...</li>
   <li value="29">...</li> 
   <li value="5">...</li> 
</ul>

JS code : JS代码:

function sortAll() {
    $(".parentClass").each(function() {
        var items = $(this).children("li").sort(function(a, b) {
        var vA = $("li", a).attr("value");
        var vB = $("li", b).attr("value");
        return (vA > vB) ? -1 : (vA > vB) ? 0 : 1;
        });
        $(this).append(items);
    });
}

What I'm trying to get : 我想要得到的是:

<ul class="parentClass">
   <li value="10">...</li>
   <li value="8">...</li> 
   <li value="1">...</li> 
</ul>
<ul class="parentClass">
   <li value="29">...</li>
   <li value="8">...</li> 
   <li value="5">...</li> 
</ul>

I think I misunderstood something with iteration. 我想我对迭代有误解。 Could you help me to figure out my mistake? 您能帮我弄清楚我的错误吗?

I would also let you know that some <li> elements could have the same value. 我还要让您知道某些<li>元素可能具有相同的值。

Solution : 解决方案:

function sortAll() {
        $(".parentClass").each(function() {
            var items = $(this).children("li").sort(function(a, b) {
            var vA = $(a).attr("value");
            var vB = $(b).attr("value");
            return (vA > vB) ? -1 : (vA > vB) ? 0 : 1;
            });
            $(this).append(items);
        });
    }

You should use $(a) and $(b) instead of the $("li", a) and $("li", b) . 您应该使用$(a)$(b)代替$("li", a)$("li", b) a and b refer to the li elements. ab表示li元素。 You are trying to find the descendant li s of the li elements ( $(selector, context) is the same as $(context).find(selector) ). 您尝试查找li元素的后代li s( $(selector, context)$(context).find(selector) )。 The queries return empty collections and subsequently attr returns an undefined value. 查询返回空集合,随后attr返回undefined值。 Both undefined > undefined and undefined < undefined return false . undefined > undefinedundefined < undefined返回false The sort callback always returns 0 which leaves the order unchanged. sort回调始终返回0 ,使顺序保持不变。

Please have a look at example of descending sorting with your data. 请看一下对数据进行降序排序的示例。 There are a good answer about array sorting with a description for algorithm. 关于算法的描述,关于数组排序有一个很好的答案。

The ternary operator (vA > vB) ? -1 : (vA > vB) ? 0 : 1 三元运算符(vA > vB) ? -1 : (vA > vB) ? 0 : 1 (vA > vB) ? -1 : (vA > vB) ? 0 : 1 (vA > vB) ? -1 : (vA > vB) ? 0 : 1 that you have used is behaving weird, resulting in wrong order. 您使用的(vA > vB) ? -1 : (vA > vB) ? 0 : 1行为异常,导致顺序错误。

  • If compareFunction(a, b) is less than 0, sort a to a lower index than b, ie a comes first. 如果compareFunction(a,b)小于0,则将a排序到比b低的索引,即a在前。
  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. 如果compareFunction(a,b)返回0,则a和b彼此相对不变,但对所有不同元素进行排序。 Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (eg Mozilla versions dating back to at least 2003) respect this. 注意:ECMAscript标准不保证此行为,因此并非所有浏览器(例如,至少可追溯到2003年的Mozilla版本)都遵守此规定。
  • If compareFunction(a, b) is greater than 0, sort b to a lower index than a. 如果compareFunction(a,b)大于0,则将b排序为比a更低的索引。 compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. 当给定一对特定的元素a和b作为其两个参数时,compareFunction(a,b)必须始终返回相同的值。
  • If inconsistent results are returned then the sort order is undefined 如果返回不一致的结果,则排序顺序不确定

If vA > vB // true sort a to a lower index than b 如果vA > vB // true是,则将a排序为比b低的索引

Therefore if vA < vB // true leave a and b unchanged. 因此,如果vA < vB // true ,则保持a和b不变。 This is a logical error. 这是一个逻辑错误。

 function sortAll() { // Define variables before loops. var $container, $itemsColl; $(".parentClass").each(function(key, value) { // <ul> tag $container = $(value); $itemsColl = $container.children("li"); // Sort and appendTo() <ul>. $itemsColl.sort(function(a, b) { // Inverted for descendant sorting. return $(b).attr('value') - $(a).attr('value'); }) .appendTo($container); }); } sortAll(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul class="parentClass"> <li value="1">1</li> <li value="10">10</li> <li value="8">8</li> </ul> <ul class="parentClass"> <li value="8">8</li> <li value="29">29</li> <li value="5">5</li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM