I have a list of items, each of which has a rank associated to it.
Item 1, Rank=1
Item 2, Rank=2
Item 3, Rank=3
Item 4, Rank=4
Item 5, Rank=5
I want to design a method which handles re-ranking of items with minimal or no changes to the ranks of other items.
One solution I thought of was to leverage decimals and use Java's double variable type. So for example if I move item5 between item2 and item3, this would be the output -
Item 1, Rank=1
Item 2, Rank=2
Item 5, Rank=2.5
Item 3, Rank=3
Item 4, Rank=4
And so on,
Item 1, Rank=1
Item 2, Rank=2
Item 4, Rank=2.25
Item 5, Rank=2.5
Item 3, Rank=3
This solutions works, but after some point (~55 moves at the same position, I reach the double variable limit and i will probably have to reset all ranks at that point)
I was just wondering if there is some better way to solve this problem?
A few things to keep in mind.
- I need to store this data structure in a database (Item, Rank) and I will be building a web service which gets all items in a sorted order based on rank so I would be making a DB call to get all items sorted by the rank field.
- I will be using Java so I can only deal with Java variables.