I need to do the following with a Java/Grails GORM application.
I have a domain class Item:
class Item {
int position
String name
}
When I create a list of items I can update the position attribute such that it represents the index of each item in the list: 0,1,2,3,... Each position is unique, i.e., at each position can only be one item.
It should be able to change the order of the items. Let's assume I have the following list of items and there positions:
A1, A2, A3, A4, A5
1 2 3 4 5
When I want A4 to be at position 2 I have to update the positions of A2, A3 and respectively. This means that I have to update three data base entries A4, A2 and A3. If the list is very long then there are a lot of updates to do.
- Is there a data structure which handler the repositioning of list elements for me?
- How can I update the items efficiently?