Suppose we have a simple database containing following data:
name
apple
pear
banana
grape
User want to sort those fruits by the name, and we will have, with no surprise
apple
banana
grape
pear
However, for some reason, user would like to place pear as the 3rd fruit ,that means he would like to have :
apple
banana
pear
grape
And, importantly, the user want to preserve this order when he want to sort the fruits by name thereafter.
How should we tackle this problem? Of top of my head, we could add a field user_sort_id, which will be updated when user sort and manipulate the sort result and we will use that filed as the sort key.
init value -> sort by name ->place pear as the seconds
name user_sort_id
apple 0 0 0
pear 1 3 2
banana 2 1 1
grape 3 2 3
This approach should work in theory. However, in practice, I can not think of an elegant and fast SQL statement that could accomplish this. Any ideas or alternatives?
If you want each user to have independent sort orders, you need another table.
Then ordering is easy.
There’s no magic bullet for updating.