I have a program I'm working on where I want to allow users to select and update the order of objects in relation to one another. The easiest way to think about it would be display order on a webpage - which object should come first. All the object details are stored in a DB, so it would be really nice if I could just order by a column and not have to worry about it anywhere else...
A few solutions I've found or come up with:
1) The old 'order_no' column
- An extra NUM column that gets a number assigned every time a field is updated/inserted
- This can be annoying/problematic and will eventually fail (requiring complete reordering of related objects every so often).
- Example, start with 10, the next item could be 20...then 30. Then someone updates the object associated with 30 to be before 20... ok.. make it 15... keep doing this though and you run out of numbers. You could resolve this by calculating the complete order on the fly (i.e. always determine what should be 1,2,3,etc and update) or do this at some interval (every hour go in and update 10->1, 15->2, 20->3, etc)... but seems like a lot of extra work...
2) Previous (or next) object relation
- The order column is a reference to the previous item in the list
- Ex) Object1 is first in the list and the order column is NULL. Object 2 is the 2nd in the list and it's order column has a reference to object1 in it...
- This is fine until you rearranged the order.. what if two objects have the same previous/next relation... how to resolve?
I'm sure there is some basic list programming principles i have missed, which is why i can't think of anything more elegant...
any ideas?
A few solutions I've found or come up with:
1) The old 'order_no' column
- An extra NUM column that gets a number assigned every time a field is updated/inserted
- This can be annoying/problematic and will eventually fail (requiring complete reordering of related objects every so often).
- Example, start with 10, the next item could be 20...then 30. Then someone updates the object associated with 30 to be before 20... ok.. make it 15... keep doing this though and you run out of numbers. You could resolve this by calculating the complete order on the fly (i.e. always determine what should be 1,2,3,etc and update) or do this at some interval (every hour go in and update 10->1, 15->2, 20->3, etc)... but seems like a lot of extra work...
2) Previous (or next) object relation
- The order column is a reference to the previous item in the list
- Ex) Object1 is first in the list and the order column is NULL. Object 2 is the 2nd in the list and it's order column has a reference to object1 in it...
- This is fine until you rearranged the order.. what if two objects have the same previous/next relation... how to resolve?
I'm sure there is some basic list programming principles i have missed, which is why i can't think of anything more elegant...
any ideas?
