Sorting nested lists with jquery





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to sort nested lists. I came across this link: Sorting <li> elements of a <Ul> according to text in <span> inside <li>



It gets me most of the way there, however, it assumes you do not have lists like



<ul>
<li><span>sortable text a</span>
<ul>
<li><span>sortable subtext</span></li>
</ul>
</li>
<ul>


Whenever I use the code suggested above, any nested list gets appended to the first <li> in the nested list. I need it to go to the original list item.



My code:






$(document).ready(function() {
// for each ul separately
$('#rootUl ul, ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li').children('span');

// extract the text
var values = $toSort.get().map(function(span) {
return span.textContent;
});

// sort the text
values.sort();

// shove reordered texts back into the original elements
values.forEach(function(value, index) {
$toSort[index].textContent = value;
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>





I would expect the following:



<ul id="rootUl">
<li>
<span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>mm ccc</span></li>
<li><span>nn ccc</span></li>
</ul>
</li>
<li>
<span>rrr</span>
<ul>
<li><span>qq rrr</span></li>
<li><span>rr rrr</span></li>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ee ss rrr</span></li>
<li><span>ff ss rrr</span></li>
</ul>
</li>
</ul>
</li>
<li>
<span>zzz</span>
<ul>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
<li><span>dd zzz</span></li>
</ul>
</li>
</ul>


But instead, I get this:



<ul id="rootUl">
<li>
<span>ccc</span>
<ul>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
<li><span>dd zzz</span></li>
</ul>
</li>
<li>
<span>rrr</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>mm ccc</span></li>
<li><span>nn ccc</span></li>
</ul>
</li>
<li><span>zzz</span>
<ul>
<li>
<span>qq rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ee ss rrr</span></li>
<li><span>ff ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>ss rrr</span></li>
</ul>
</li>
</ul>









share|improve this question

























  • You're just sorting the text of each span at the same level, you're not moving the rest of the li with it.

    – Barmar
    Jan 3 at 22:29











  • Thanks for the information, but I'm not having any luck moving the rest of the li to its original <ul> tag. Can you tell me how?

    – Jonathan M Roe
    Jan 4 at 14:50


















0















I am trying to sort nested lists. I came across this link: Sorting <li> elements of a <Ul> according to text in <span> inside <li>



It gets me most of the way there, however, it assumes you do not have lists like



<ul>
<li><span>sortable text a</span>
<ul>
<li><span>sortable subtext</span></li>
</ul>
</li>
<ul>


Whenever I use the code suggested above, any nested list gets appended to the first <li> in the nested list. I need it to go to the original list item.



My code:






$(document).ready(function() {
// for each ul separately
$('#rootUl ul, ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li').children('span');

// extract the text
var values = $toSort.get().map(function(span) {
return span.textContent;
});

// sort the text
values.sort();

// shove reordered texts back into the original elements
values.forEach(function(value, index) {
$toSort[index].textContent = value;
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>





I would expect the following:



<ul id="rootUl">
<li>
<span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>mm ccc</span></li>
<li><span>nn ccc</span></li>
</ul>
</li>
<li>
<span>rrr</span>
<ul>
<li><span>qq rrr</span></li>
<li><span>rr rrr</span></li>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ee ss rrr</span></li>
<li><span>ff ss rrr</span></li>
</ul>
</li>
</ul>
</li>
<li>
<span>zzz</span>
<ul>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
<li><span>dd zzz</span></li>
</ul>
</li>
</ul>


But instead, I get this:



<ul id="rootUl">
<li>
<span>ccc</span>
<ul>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
<li><span>dd zzz</span></li>
</ul>
</li>
<li>
<span>rrr</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>mm ccc</span></li>
<li><span>nn ccc</span></li>
</ul>
</li>
<li><span>zzz</span>
<ul>
<li>
<span>qq rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ee ss rrr</span></li>
<li><span>ff ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>ss rrr</span></li>
</ul>
</li>
</ul>









share|improve this question

























  • You're just sorting the text of each span at the same level, you're not moving the rest of the li with it.

    – Barmar
    Jan 3 at 22:29











  • Thanks for the information, but I'm not having any luck moving the rest of the li to its original <ul> tag. Can you tell me how?

    – Jonathan M Roe
    Jan 4 at 14:50














0












0








0








I am trying to sort nested lists. I came across this link: Sorting <li> elements of a <Ul> according to text in <span> inside <li>



It gets me most of the way there, however, it assumes you do not have lists like



<ul>
<li><span>sortable text a</span>
<ul>
<li><span>sortable subtext</span></li>
</ul>
</li>
<ul>


Whenever I use the code suggested above, any nested list gets appended to the first <li> in the nested list. I need it to go to the original list item.



My code:






$(document).ready(function() {
// for each ul separately
$('#rootUl ul, ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li').children('span');

// extract the text
var values = $toSort.get().map(function(span) {
return span.textContent;
});

// sort the text
values.sort();

// shove reordered texts back into the original elements
values.forEach(function(value, index) {
$toSort[index].textContent = value;
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>





I would expect the following:



<ul id="rootUl">
<li>
<span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>mm ccc</span></li>
<li><span>nn ccc</span></li>
</ul>
</li>
<li>
<span>rrr</span>
<ul>
<li><span>qq rrr</span></li>
<li><span>rr rrr</span></li>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ee ss rrr</span></li>
<li><span>ff ss rrr</span></li>
</ul>
</li>
</ul>
</li>
<li>
<span>zzz</span>
<ul>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
<li><span>dd zzz</span></li>
</ul>
</li>
</ul>


But instead, I get this:



<ul id="rootUl">
<li>
<span>ccc</span>
<ul>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
<li><span>dd zzz</span></li>
</ul>
</li>
<li>
<span>rrr</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>mm ccc</span></li>
<li><span>nn ccc</span></li>
</ul>
</li>
<li><span>zzz</span>
<ul>
<li>
<span>qq rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ee ss rrr</span></li>
<li><span>ff ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>ss rrr</span></li>
</ul>
</li>
</ul>









share|improve this question
















I am trying to sort nested lists. I came across this link: Sorting <li> elements of a <Ul> according to text in <span> inside <li>



It gets me most of the way there, however, it assumes you do not have lists like



<ul>
<li><span>sortable text a</span>
<ul>
<li><span>sortable subtext</span></li>
</ul>
</li>
<ul>


Whenever I use the code suggested above, any nested list gets appended to the first <li> in the nested list. I need it to go to the original list item.



My code:






$(document).ready(function() {
// for each ul separately
$('#rootUl ul, ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li').children('span');

// extract the text
var values = $toSort.get().map(function(span) {
return span.textContent;
});

// sort the text
values.sort();

// shove reordered texts back into the original elements
values.forEach(function(value, index) {
$toSort[index].textContent = value;
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>





I would expect the following:



<ul id="rootUl">
<li>
<span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>mm ccc</span></li>
<li><span>nn ccc</span></li>
</ul>
</li>
<li>
<span>rrr</span>
<ul>
<li><span>qq rrr</span></li>
<li><span>rr rrr</span></li>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ee ss rrr</span></li>
<li><span>ff ss rrr</span></li>
</ul>
</li>
</ul>
</li>
<li>
<span>zzz</span>
<ul>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
<li><span>dd zzz</span></li>
</ul>
</li>
</ul>


But instead, I get this:



<ul id="rootUl">
<li>
<span>ccc</span>
<ul>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
<li><span>dd zzz</span></li>
</ul>
</li>
<li>
<span>rrr</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>mm ccc</span></li>
<li><span>nn ccc</span></li>
</ul>
</li>
<li><span>zzz</span>
<ul>
<li>
<span>qq rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ee ss rrr</span></li>
<li><span>ff ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>ss rrr</span></li>
</ul>
</li>
</ul>





$(document).ready(function() {
// for each ul separately
$('#rootUl ul, ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li').children('span');

// extract the text
var values = $toSort.get().map(function(span) {
return span.textContent;
});

// sort the text
values.sort();

// shove reordered texts back into the original elements
values.forEach(function(value, index) {
$toSort[index].textContent = value;
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>





$(document).ready(function() {
// for each ul separately
$('#rootUl ul, ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li').children('span');

// extract the text
var values = $toSort.get().map(function(span) {
return span.textContent;
});

// sort the text
values.sort();

// shove reordered texts back into the original elements
values.forEach(function(value, index) {
$toSort[index].textContent = value;
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>






jquery list nested






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 21:47









Andreas

16.7k52842




16.7k52842










asked Jan 3 at 21:34









Jonathan M RoeJonathan M Roe

11




11













  • You're just sorting the text of each span at the same level, you're not moving the rest of the li with it.

    – Barmar
    Jan 3 at 22:29











  • Thanks for the information, but I'm not having any luck moving the rest of the li to its original <ul> tag. Can you tell me how?

    – Jonathan M Roe
    Jan 4 at 14:50



















  • You're just sorting the text of each span at the same level, you're not moving the rest of the li with it.

    – Barmar
    Jan 3 at 22:29











  • Thanks for the information, but I'm not having any luck moving the rest of the li to its original <ul> tag. Can you tell me how?

    – Jonathan M Roe
    Jan 4 at 14:50

















You're just sorting the text of each span at the same level, you're not moving the rest of the li with it.

– Barmar
Jan 3 at 22:29





You're just sorting the text of each span at the same level, you're not moving the rest of the li with it.

– Barmar
Jan 3 at 22:29













Thanks for the information, but I'm not having any luck moving the rest of the li to its original <ul> tag. Can you tell me how?

– Jonathan M Roe
Jan 4 at 14:50





Thanks for the information, but I'm not having any luck moving the rest of the li to its original <ul> tag. Can you tell me how?

– Jonathan M Roe
Jan 4 at 14:50












1 Answer
1






active

oldest

votes


















0














You need to sort the li elements within their parent lists, not just move the text contents of the spans. Use a callback function in sort() that compares the text of the span.



Once you've sorted the lis, you can append them to their parent ul to rearrange them in the DOM.



It's also not necessary to specify both #rootUl ul and ul in the selector, since ul will match #rootUl ul.






$(document).ready(function() {
// for each ul separately
$('ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li');
$toSort.sort((li1, li2) => $(li1).children("span").text().localeCompare($(li2).children("span").text()));
$toSort.each(function() {
$(this).appendTo(ul);
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>








share|improve this answer
























  • That's got it! Thank you!

    – Jonathan M Roe
    Jan 4 at 19:41












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030113%2fsorting-nested-lists-with-jquery%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to sort the li elements within their parent lists, not just move the text contents of the spans. Use a callback function in sort() that compares the text of the span.



Once you've sorted the lis, you can append them to their parent ul to rearrange them in the DOM.



It's also not necessary to specify both #rootUl ul and ul in the selector, since ul will match #rootUl ul.






$(document).ready(function() {
// for each ul separately
$('ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li');
$toSort.sort((li1, li2) => $(li1).children("span").text().localeCompare($(li2).children("span").text()));
$toSort.each(function() {
$(this).appendTo(ul);
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>








share|improve this answer
























  • That's got it! Thank you!

    – Jonathan M Roe
    Jan 4 at 19:41
















0














You need to sort the li elements within their parent lists, not just move the text contents of the spans. Use a callback function in sort() that compares the text of the span.



Once you've sorted the lis, you can append them to their parent ul to rearrange them in the DOM.



It's also not necessary to specify both #rootUl ul and ul in the selector, since ul will match #rootUl ul.






$(document).ready(function() {
// for each ul separately
$('ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li');
$toSort.sort((li1, li2) => $(li1).children("span").text().localeCompare($(li2).children("span").text()));
$toSort.each(function() {
$(this).appendTo(ul);
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>








share|improve this answer
























  • That's got it! Thank you!

    – Jonathan M Roe
    Jan 4 at 19:41














0












0








0







You need to sort the li elements within their parent lists, not just move the text contents of the spans. Use a callback function in sort() that compares the text of the span.



Once you've sorted the lis, you can append them to their parent ul to rearrange them in the DOM.



It's also not necessary to specify both #rootUl ul and ul in the selector, since ul will match #rootUl ul.






$(document).ready(function() {
// for each ul separately
$('ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li');
$toSort.sort((li1, li2) => $(li1).children("span").text().localeCompare($(li2).children("span").text()));
$toSort.each(function() {
$(this).appendTo(ul);
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>








share|improve this answer













You need to sort the li elements within their parent lists, not just move the text contents of the spans. Use a callback function in sort() that compares the text of the span.



Once you've sorted the lis, you can append them to their parent ul to rearrange them in the DOM.



It's also not necessary to specify both #rootUl ul and ul in the selector, since ul will match #rootUl ul.






$(document).ready(function() {
// for each ul separately
$('ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li');
$toSort.sort((li1, li2) => $(li1).children("span").text().localeCompare($(li2).children("span").text()));
$toSort.each(function() {
$(this).appendTo(ul);
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>








$(document).ready(function() {
// for each ul separately
$('ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li');
$toSort.sort((li1, li2) => $(li1).children("span").text().localeCompare($(li2).children("span").text()));
$toSort.each(function() {
$(this).appendTo(ul);
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>





$(document).ready(function() {
// for each ul separately
$('ul').each(function(_, ul) {
// get all the nodes to be sorted
var $toSort = $(ul).children('li');
$toSort.sort((li1, li2) => $(li1).children("span").text().localeCompare($(li2).children("span").text()));
$toSort.each(function() {
$(this).appendTo(ul);
});
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="rootUl">
<li><span>zzz</span>
<ul>
<li><span>dd zzz</span></li>
<li><span>aa zzz</span></li>
<li><span>bb zzz</span></li>
</ul>
</li>
<li><span>ccc</span>
<ul>
<li><span>ll ccc</span></li>
<li><span>nn ccc</span></li>
<li><span>mm ccc</span></li>
</ul>
</li>
<li><span>rrr</span>
<ul>
<li><span>ss rrr</span>
<ul>
<li><span>dd ss rrr</span></li>
<li><span>ff ss rrr</span></li>
<li><span>ee ss rrr</span></li>
</ul>
</li>
<li><span>rr rrr</span></li>
<li><span>qq rrr</span></li>
</ul>
</li>
</ul>






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 4 at 18:36









BarmarBarmar

435k36260363




435k36260363













  • That's got it! Thank you!

    – Jonathan M Roe
    Jan 4 at 19:41



















  • That's got it! Thank you!

    – Jonathan M Roe
    Jan 4 at 19:41

















That's got it! Thank you!

– Jonathan M Roe
Jan 4 at 19:41





That's got it! Thank you!

– Jonathan M Roe
Jan 4 at 19:41




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030113%2fsorting-nested-lists-with-jquery%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'