title: How can I sort an array, yet exclude certain elements (to be kept at the same position in the array)
body: <p>This will be implemented in Javascript (jQuery) but I suppose the method could be used in any language.</p>
  
  <p>I have an array of items and I need to perform a sort. <strong>However</strong> there are some items in the array that have to be kept in the same position (same index).</p>
  
  <p>The array in question is build from a list of <code>&lt;li&gt;</code> elements and I'm using .data() values attached to the list item as the value on which to sort.</p>
  
  <p>What approach would be best here?</p>
  
  <pre><code>&lt;ul id="fruit"&gt;
    &lt;li class="stay"&gt;bananas&lt;/li&gt;
    &lt;li&gt;oranges&lt;/li&gt;
    &lt;li&gt;pears&lt;/li&gt;
    &lt;li&gt;apples&lt;/li&gt;
    &lt;li class="stay"&gt;grapes&lt;/li&gt;
    &lt;li&gt;pineapples&lt;/li&gt;
  &lt;/ul&gt;
  
  &lt;script type="text/javascript"&gt;
      var sugarcontent = new Array('32','21','11','45','8','99');
      $('#fruit li').each(function(i,e){
         $(this).data('sugar',sugarcontent[i]);
      })
  &lt;/script&gt;
  </code></pre>
  
  <p>I want the list sorted with the following result...</p>
  
  <pre><code>&lt;ul id="fruit"&gt;
        &lt;li class="stay"&gt;bananas&lt;/li&gt; &lt;!-- score = 32 --&gt;
        &lt;li&gt;pineapples&lt;/li&gt; &lt;!-- score = 99 --&gt;
        &lt;li&gt;apples&lt;/li&gt; &lt;!-- score = 45 --&gt;
        &lt;li&gt;oranges&lt;/li&gt; &lt;!-- score = 21 --&gt;
        &lt;li class="stay"&gt;grapes&lt;/li&gt; &lt;!-- score = 8 --&gt;
        &lt;li&gt;pears&lt;/li&gt; &lt;!-- score = 11 --&gt;
    &lt;/ul&gt;
  </code></pre>
  
  <p>Thanks!</p>