对嵌套列表进行排序 [英] Sorting nested lists
问题描述
给定是一个嵌套列表,带有以下标记(此时遗憾地无法更改)。
我想对这个列表进行排序(以及'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);
});
});
这篇关于对嵌套列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!