title: Sort dynamically created div based on their id using jquery
body: <p><strong>Sort dynamically created div based on their id.</strong></p>
  
  <p>I have the <code>html</code> structure like below:</p>
  
  <pre><code>&lt;div id="res"&gt;
    &lt;div id="3"&gt;text 3&lt;/div&gt;
    &lt;div id="4"&gt;text 4&lt;/div&gt;
    &lt;div id="1"&gt;text 1&lt;/div&gt;
    &lt;div id="2"&gt;text 2&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>These <code>html are created dynamically</code>.</p>
  
  <pre><code>$("#res").append("&lt;div id="+i+"&gt;text "+i+"&lt;/div&gt;");
  </code></pre>
  
  <p>The order may vary(it is created using <strong>ajax</strong> in <strong>for</strong> loop). <strong>Unluckly divs are displayed in random order</strong>.(I don't know the reason).</p>
  
  <p>However after appending the <code>divs</code> to the <code>res</code> <code>div</code>, I decided to sort the divs in correct order based on their <code>id</code>.</p>
  
  <p>So the expected result should be:</p>
  
  <pre><code>&lt;div id="res"&gt;
    &lt;div id="1"&gt;text 1&lt;/div&gt;
    &lt;div id="2"&gt;text 2&lt;/div&gt;
    &lt;div id="3"&gt;text 3&lt;/div&gt;
    &lt;div id="4"&gt;text 4&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>How can I do this?</p>
  
  <p>I tried  </p>
  
  <pre><code>$("#resdiv").tsort("",{attr:"id"});
  </code></pre>
  
  <p>and </p>
  
  <pre><code>$('#res&gt; div').toArray().sort( function(a,b) { a.id - b.id } );
  </code></pre>
  
  <p>But nothing worked...</p>
  
  <p>Someone can help me..</p>