title: how to reorder a list of objects?
body: <p>Firstly, this is going to sound like homework, but it ain't.  Just a problem I'm trying to solve at work.</p>
  
  <p>I have a list of objects, the objects have a sequence number which indicates their order in the list in the UI.  Example:</p>
  
  <pre><code>public class Task {
    Long id;
    String name;
    Long seq;
  }
  </code></pre>
  
  <p>The table in my UI has "up" and "down" links on each row of the table for moving the tasks up and down in the list.</p>
  
  <p>I'm implementing two methods to handle the reordering.  These methods are call by ajax in the web UI.</p>
  
  <pre><code>public void incTaskSeq(List&lt;Task&gt; allTasks, Task taskToMove)
  </code></pre>
  
  <p>For example; if I have t1.seq=1, t2.seq=2, t3.seq=3, t4.seq=4, t5.seq=5 and I want to increment the place of t3, then t3.seq becomes 4, and t4.seq must become 3.</p>
  
  <pre><code>public void decTaskSeq(List&lt;Task&gt; allTasks, Task taskToMove)
  </code></pre>
  
  <p>Similarly; if I have t1.seq=1, t2.seq=2, t4.seq=3, t3.seq=4, t5.seq=5 and I want to decrement the place of t4, then t4.seq becomes 2, and t2.seq must become 3.  Resulting in:</p>
  
  <p>t1.seq=1, t4.seq=2, t2.seq=3, t3.seq=4, t5.seq=5</p>
  
  <p>I'm a little stuck on the best way to do this.</p>
  
  <p>I was thinking of putting all the tasks in a HashMap and then sort the map by the sequence number.  Then locate the taskToMove in the map, change the sequence number, and then change all the affected tasks sequences.</p>
  
  <p>But this approach seems inelegant.  Does anyone have any ideas how I should do this?</p>
  
  <p>Thanks, Rob</p>