I have a Java list of objects which periodically is cleared then reloaded with updated versions of the objects from a database. Before reloading, the user may have chosen to order the list in a certain way. I need to find an algorithm which applies the order of the previous list to the fresh list.
I cannot use Comparator as the object may effectively be in a random order.
Here is my method stub:
public static List<RetrievedPage> copyPreviousListOrderToFreshList(List<RetrievedPage> previousCopyOfList, List<RetrievedPage> freshCopyOfList)
{
for (RetrievedPage retrievedPage : previousCopyOfList)
{
//reordering, but how?
}
return freshCopyOfList;
}
Thanks in advance,
Barry
You can still use a comparator, just compare the items depending on their position in the previous list.
Of course, if you have different objects in the lists, you shouldn’t forget to override
equals()andhashCode()methods in the objects you compare. OtherwiseindexOf()method will look for exactly the same object, and won’t test them for equality the way you intend.