Okay, this has bugged me for a long time.
Imagine a list held in a database table, where I want to control the sort order of the items, so that I can present the list in the order I want.
Well I could add a sortOrder column and give each item a value, 1,2,3,....5000
Now I can select and use ORDER BY sortOrder ASC
Great, but now I want the users to be able to move any item up or down the list. I could just increment/decrement sortOrder by one, but then two items would share the same sort order which is not ideal. I could renumber the sortOrder for all the items, but I may have 10,000,000 items.
So the question is, what is the optimal solution to be able to have a user controled sort order, where each item can be moved up or down the list and also be given a specific position (e.g. moved from position 3 to position 546)?
It is important that retrieving the items in the correct order is very fast and simple SQL.
Imagine a list held in a database table, where I want to control the sort order of the items, so that I can present the list in the order I want.
Well I could add a sortOrder column and give each item a value, 1,2,3,....5000
Now I can select and use ORDER BY sortOrder ASC
Great, but now I want the users to be able to move any item up or down the list. I could just increment/decrement sortOrder by one, but then two items would share the same sort order which is not ideal. I could renumber the sortOrder for all the items, but I may have 10,000,000 items.
So the question is, what is the optimal solution to be able to have a user controled sort order, where each item can be moved up or down the list and also be given a specific position (e.g. moved from position 3 to position 546)?
It is important that retrieving the items in the correct order is very fast and simple SQL.

Comment