对嵌套列表进行排序

编程入门 行业动态 更新时间:2024-10-09 11:20:06
本文介绍了对嵌套列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给定是一个嵌套列表,带有以下标记(此时遗憾地无法更改)。 我想对这个列表进行排序(以及'a'-tag标题所有嵌套列表。)第一个div(不是嵌套在'li'中)应该用来排序'ul' '-siblings。

Given is a nested list, with the following markup (and sadly can't be changed at this moment). I want to sort this list (and all nested lists by the 'a'-tag title.) The first div (which is not nested in 'li') should be used to sort the 'ul'-siblings.

性能也很重要,因为这个列表可以轻松地包含超过100个项目,但比我想的少1000个。(我不确定这是否可能会成为性能问题)

Performance is also important, as this list can easly contain more than 100 items, but less than I guess 1000. (I'm not sure if this might become a performance issue)

<ul> <div class="bioResult"><a href="">Test</a></div> </ul> <ul> <li> <ul> <div class="bioResult"><a href="">C Departmnet</a></div> <li> <div class="bioResult"><a href="">C</a></div> </li> </ul> </li> </ul>

我已经挣扎了并完成了嵌套列表排序,但我无法理解'ul'-siblings排序,因为它不起作用,因为我认为它可以工作。 :)

I already struggled around and got the nested list sorting done, but I couldn't get my head around the 'ul'-siblings sorting, as it didn't work as I thought it could work. :)

请参阅我的小提琴。

此时插件不是一个选项。

Plugins aren't an option at this moment.

推荐答案

排序一切都是按字母顺序将所有节点附加到各自父母的问题。

Sorting everything is a matter of appending all nodes to their respective parents in alphabetical order.

function uCase(elem) { return $.trim( $(elem).text().toUpperCase() ) } function compareFirstLink(a, b) { var A = uCase( $(a).first('a') ), B = uCase( $(b).first('a') ); return (A > B) ? 1 : -1; } $(function () { var $sortables = $("ul:has(div:first-child), li:not(.fixedOrder)"); $sortables.sort(compareFirstLink).each(function () { this.parentNode.appendChild(this); }); });

更多推荐

对嵌套列表进行排序

本文发布于:2023-11-29 09:02:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!