title: jQuery inserting children first when using append() instead of last
body: <p>I'm having trouble inserting child elements to a list in HTML using jQuery. I'm a beginner when it comes to anything JavaScript related so my code is probably very clunky and unoptimized. Even if you can't help me with the original issue I always appriciate pointers to improve my code and learn.</p>
  <p>I have a function that runs every second (it's called from my CefSharp application, not sure if it's relevant). This is my function:</p>
  <pre><code>function updateOrderQueue(index, reference, pickedup, deliveredto, robotId, statuscode, statustext) {
      var list = $('#orderQueueList');
      var count = $(&quot;#orderQueueList li&quot;).length;
  
      if (!document.getElementById('order' + reference) &amp;&amp; statuscode &lt; 4 &amp;&amp; count &lt; 10) {
          var data = $(`&lt;li class=&quot;list-group-item d-flex justify-content-between align-items-center border border-dark&quot; id=&quot;order` + reference + `&quot;&gt;
                          &lt;h3 class=&quot;mt-1&quot; id=&quot;text`+ reference + `&quot;&gt;
                              [`+index+`] `+ pickedup + ` ➔ ` + deliveredto + ` (` + statustext +  `)
                          &lt;/h3&gt;
                          &lt;small class=&quot;badge badge-dark rounded&quot; style=&quot;font-size:24px;&quot; id=&quot;robotQueue`+ reference + `&quot;&gt;` + robotId + `&lt;/small&gt;
                      &lt;/li&gt;`);
          list.append(data).hide().fadeIn(500);
      } else {
          var locations = jQuery(&quot;#text&quot; + jq(reference));
          var assignedrobot = jQuery(&quot;#robotQueue&quot; + jq(reference));
  
          locations.text('['+index+'] '+ pickedup + ' ➔ ' + deliveredto + ' (' + statustext + ')');
          assignedrobot.text(robotId);
      }
  
      var elems = $('#orderQueueList li').detach().sort(function (a, b) {
          if (isNaN($(a).text()) || isNaN($(b).text())) {
              return $(a).text() &gt; $(b).text() ? 1 : -1;
          }
          return $(a).text() - $(b).text();
      });
      list.append(elems);
  }
  </code></pre>
  <p>And this is the HMTL code</p>
  <pre><code>&lt;div id=&quot;orderQueue&quot; class=&quot;&quot;&gt;
     &lt;ul class=&quot;list-group&quot; id=&quot;orderQueueList&quot;&gt;
     &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  <p>I'm going to try to quickly explain what I intend on it doing and what it actually does.</p>
  <p>So basically, this code is supposed to <strong>add</strong>, <strong>sort</strong> and <strong>add relevant classes</strong> to items in a list. To be added, the item needs to pass a set of requirements (not already added, statuscode under 4 and not more than 10 items total). I then append the item into the list, which I assumed would put it at the end of the list (compared to prepend, which should do the opposite).</p>
  <p>The problem is, whenever a new item is added, it is added to the top of the list. My guess is that is has something to do with the fact that I'm appending it to the <strong>ul</strong>. I tried doing something like <strong>$('#orderQueueList li:last')</strong> but I didn't get it to work. I've also tried to do <strong>$('#orderQueueList).children()</strong> and <strong>$('#orderQueueList).last()</strong> before appending without success.</p>
  <p>What am I missing? Have I completely misunderstood what append does? Have I written the code wrong?</p>
  <p>Appriciate any pointers,</p>
  <p>Thank you in advance.</p>