title: Algorithm to re-index an array of objects after insertion or drag 'n' drop order change
body: <p>Assume I have an indexed array of objects, such as these containing lines of a popular folk song ;)</p>
  
  <pre><code>var lyrics = [
    {line : 2, words : "He's a lumberjack and he's okay"},
    {line : 1, words : "I'm a lumberjack and I'm okay"},
    {line : 3, words : "He sleeps all night and he works all day"}
  ];
  </code></pre>
  
  <p>My comparator will display the objects in the view according to each object's index. I want to be able to perform the three tasks on this array:</p>
  
  <p>Task 1) Reindex on drag 'n drop</p>
  
  <p>Re-arrange the order of the objects via drag and drop. Assume I already know how to implement drag and drop. Task example: Drag "He's a lumberjack and he's okay" from index "1" to after "I'm a lumberjack and I'm okay". "He's a lumberjack and he's okay" should now occupy index "2" and "I'm a lumberjack and I'm okay" should occupy index "1". The resulting array should be:</p>
  
  <pre><code>var lyrics = [
    {line : 1, words : "I'm a lumberjack and I'm okay"},
    {line : 2, words : "He's a lumberjack and he's okay"},
    {line : 3, words : "He sleeps all night and he works all day"}
  ];
  </code></pre>
  
  <p>Task 2) Re-index on insert</p>
  
  <p>Add an object to any point in the array, reindexing all the items in the array. Task example: Add a "I sleep all night and I work all day" object as the second item in the array. The resulting array should be:</p>
  
  <pre><code>var lyrics = [
    {line : 1, words : "I'm a lumberjack and I'm okay"},
    {line : 2, words : "I sleep all night and I work all day"},
    {line : 3, words : "He's a lumberjack and he's okay"},
    {line : 4, words : "He sleeps all night and he works all day"}
  ];
  </code></pre>
  
  <p>Task 3) Re-index on delete</p>
  
  <p>Delete an object from an array and reindex all the items in the array. So for example, if object with index "3" were deleted, the resulting array should be:</p>
  
  <pre><code>var lyrics = [
    {line : 1, words : "I'm a lumberjack and I'm okay"},
    {line : 2, words : "I sleep all night and I work all day"},
    {line : 3, words : "He sleeps all night and he works all day"}
  ];
  </code></pre>
  
  <p>I don't have a CS degree so I'm kind of stumped on what algorithm would help me handle this. Can someone point me in the right direction?</p>
  
  <p>I'm working with javascript, so if anyone knows anything that does the above, I'm all ears.</p>