Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One of the problems you'll have immediately is that with trying to sort <code>.element</code> by children is that the <code>H1</code> tags are not children of <code>.element</code></p> <pre><code>&lt;div class="element"&gt;&lt;!--Single element--&gt; &lt;div class="child1"&gt; &lt;/div&gt; &lt;div class="child2"&gt; &lt;div class="stats right"&gt; &lt;div class="stat"&gt; &lt;h1&gt;&lt;/h1&gt; &lt;h4&gt;&lt;/h4&gt; &lt;/div&gt; &lt;div class="stat"&gt; &lt;h1&gt;&lt;/h1&gt; &lt;h4&gt;&lt;/h4&gt; &lt;/div&gt; &lt;div class="stat"&gt; &lt;h1&gt;&lt;/h1&gt; &lt;h4&gt;&lt;/h4&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Looking at this element <code>.child1</code> has no children, <code>.child2</code> has 1 child and that is <code>.stats</code> the div with 2 classes <code>.stats and .right</code> has 3 children divs and those divs each have child <code>h1</code> and <code>h4</code> tags</p> <pre><code>var items = parent.children(childSelector).sort(function(a, b) </code></pre> <p>only attempts to sort 1 layer down</p> <p>If you want to keep all the nesting one simple way to fix it would be to use <code>.find</code> instead of <code>.children</code></p> <p>EDIT: Fiddle that shows another way of doing sort, this time with data nested inside the children of the children you're trying to sort; probably not that great but it works and gives you an idea of possible implementations (i.e. there is room for improvement there) <a href="http://jsfiddle.net/tc5dc/702/" rel="nofollow">http://jsfiddle.net/tc5dc/702/</a></p> <p>Fiddle code:</p> <pre><code>&lt;div class="container"&gt; &lt;div class="element"&gt; &lt;div class="stat"&gt; &lt;h1 class="inv"&gt;0&lt;/h1&gt; &lt;h4 id="mInv" class="sort"&gt;Inv&lt;/h4&gt; &lt;/div&gt; &lt;div class="stat"&gt; &lt;h1 class="ts"&gt;66&lt;/h1&gt; &lt;h4 id="mTs" class="sort"&gt;TS&lt;/h4&gt; &lt;/div&gt; &lt;div class="stat"&gt; &lt;h1 class="con"&gt;14&lt;/h1&gt; &lt;h4 id="mCon" class="sort"&gt;Con&lt;/h4&gt; &lt;/div&gt; &lt;/div&gt; $("div.stat").click(function () { var list = $(".element").children("div"); list.sort(sortDesc); $(".container").html(""); $(".container").append(list); }); function sortDesc(a, b) { var test1 = jQuery(a); var test2 = jQuery(b); return test1.find("h1").text() &gt; test2.find("h1").text(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload