cjstewart88/Tubalr — pull request #23 by ewoodh2o (2012-12-07 16:03:48+00:00)
Title: Faster video sorting

This improves @jkronz's horrendous O(n^2) reordering algorithm. (Sorry Justin!)

The old method was doing N database SELECTs to find the videos as provided by the front-end, and potentially N UPDATEs. Then, for each N, it was doing another SELECT and potentially N additional UPDATEs to guarantee `track_number` consistency after each item was moved.

This method ignores the `track_number` as presented from the client, and instead just assumes all tracks are submitted in order. One SELECT is made to fetch all the videos, then one UPDATE for each video that has changed position is performed to assign its new `track_number` based on its position in the array. Any videos belonging to the playlist that are not included in the list submitted from the client are placed at the end of the playlist.