Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6790977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:47:27+00:00 2026-05-26T17:47:27+00:00

I have a Java list of objects which periodically is cleared then reloaded with

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:47:28+00:00Added an answer on May 26, 2026 at 5:47 pm

    You can still use a comparator, just compare the items depending on their position in the previous list.

    Collections.sort(freshCopyOfList, new Comparator<RetrievedPage>() {
        public int compare(RetrievedPage o1, RetrievedPage o2) {
            int firstPagePosition = previousCopyOfList.indexOf(o1);
            int secondPagePosition = previousCopyOfList.indexOf(o2);
    
            if (secondPagePosition == -1) return -1;
            if (firstPagePosition == -1) return 1;
    
            return firstPagePosition - secondPagePosition;
        }
    });
    

    Of course, if you have different objects in the lists, you shouldn’t forget to override equals() and hashCode() methods in the objects you compare. Otherwise indexOf() method will look for exactly the same object, and won’t test them for equality the way you intend.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List of Java objects on my server which is sent to
I am trying to output some Java objects as JSON, they have List properties
Hi I have a page in my java/jsp based web application which shows list
I've a java web server application which updates objects to the database every 10
I have an object (Ticket), which has a list of other objects (Message). Message
I have a list of specific objects(meetings) which I need to group using a
In java i am writing some DTO objects, which all inherit from AllocationDTO .
I have a function returning a list of objects which ID is within a
I have an ArrayList , which I want to divide into smaller List objects
From ImmutableList javadocs: Unlike Collections.unmodifiableList(java.util.List), which is a view of a separate collection that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.