title: Efficient and correct strategy for handling sortable items in a database
body: <p>In a web application I have a set of entities in a database with a numeric field responsible for their ordering. On a client side these entities are displayed as a sortable list allowing users to change their order.</p>
  
  <p>As of now I have two solutions for managing order updates and none of them satisfies me.</p>
  
  <p>The first one is simple: every time user changes some item order, traverse updated list, get all items IDs into array, send it to a server and issue a series of updates where each item order is it's ID's index in array.<br>
  Drawbacks of this approach: lots of unnecessary updates, inability to correctly handle a situation when items array sent to a server does not contain all the entities IDs.</p>
  
  <p>The second one is as follows: when user changes an item order, changed item's ID is sent to a server along with ID's of items that "surround" changed item in it's new place in list. On server item's new order is calculated by <code>(previous.order + next.order) / 2</code>. So if item with order 3 gets moved between items with orders 5 and 6, it's new order becomes 5.5<br>
  This solution requires only one update per change but also has a serious problem: due to the algorithm used every change increases the decimal part in order numbers and sooner or later it requires more precision then my database can provide (I use MongoDB for this case but I suppose it's not that important).</p>
  
  <p>My question is if any other more efficient and correct ways exist or maybe my current approaches can be somehow improved?</p>