I have a table I'd like to sort with a "priority" column. This column needs to be reordered when the priority of a record is changed or records are removed. Think of it as an array. The values will be modified in a UI so I want them to remain whole numbers and represent the true position within the larger recordset. The priority column won't have NULLs.
id priority
1 2
2 1
3 4
4 3
Now say I change the priority of id 4 to 2 or I insert or delete a row how do I get all priorities to reshuffle so there are no gaps or duplicates and the highest possible priority is always the number of rows?
The table has a "date_modified" field which is accurate to the second and updated on insert/update so if needed it is possible to know which record was modified last (to break a tie when 2 records have the same priority)