title: sorting elements in a stackpanel WPF
body: <p>I have a stackpanel with some usercontrols that are added or removed during runtime.
  These elements have an index that i assign to them when i new them, I need to keep these elements sorted by that index so i wote a quicksort function that sorts them based on the index
  but on the line that does the swapping </p>
  
  <pre><code>          y = items[i]; //y is a temp variable
            items[i] = items[j];
  </code></pre>
  
  <p>I get</p>
  
  <p>"Specified index is already in use. Disconnect the Visual child at the specified index first"</p>
  
  <p>I tried copying them to a temp variable, delete them from the collection and then assign them to their right index with the Insert function in the UIElementCollection, but then I Get</p>
  
  <p>"Specified Visual is already a child of another Visual or the root of a CompositionTarget"</p>
  
  <p>Is there a clone element that i need or something im missing somewhere?</p>