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 8045891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:39:51+00:00 2026-06-05T05:39:51+00:00

I have 2 arraylists contained objects like this: 1st arraylist contains string and double

  • 0

I have 2 arraylists contained objects like this:

1st arraylist contains string and double : [a 1.1, b 1.2, d 0.4, g 1.9, f 0.5 and so on…]

2nd arraylist contains string, double and int : [b 0.1 2, f 1.1 5, g 2.3 1, a 1.1 2 and so on…]

2nd arraylist may contain either same or greater or lesser number of items in it than 1st one but all its string property values will be any of those which are only present in 1st one’s string property.

All I need to sort the 2nd arraylist by the string property according to the order of 1st arraylist string property. 1st arraylist is already in order and if I sort the 2nd one then the expected output will be:

[a 1.1 2, b 0.1 2, g 2.3 1, f 1.1 5 …..]

I’ve seen some questions and answers with comparator but all of them are doing sort with one type of arraylist. But here I have 2 types of arraylist and I really don’t know how to do it!

So if you have any idea please share it with me and if possible share some sample code.

I am a novice in stackoverflow as well as have not much experience in Java. I’m having a great trouble with this situation. After googling and searching particularly in stackoverflow but couldn’t get any helpful idea to go through. I’ve explained the situation above and please pardon me if I made any mistake!

Thanks!

  • 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-06-05T05:39:52+00:00Added an answer on June 5, 2026 at 5:39 am

    Lets assume the object in list1 and list2 are of type Foo and Bar and they both implement a method getString() which returns the string property. Like you said, the string values in list 1 are unique.

    ArrayList<Foo> list1 = new ArrayList<Foo>();
    ArrayList<Bar> list2 = new ArrayList<Bar>();
    
    final HashMap<String, Integer> positions = new HashMap<String, Integer>();
    for(int i=0;i<list1.size();i++){
        positions.put(obj.getString(), i);
    }
    
    Collections.sort(list2, new Comparator<Bar>(){
        public int compare(Bar obj1, Bar obj2){
            return positions.get(obj2.getString()) - positions.get(obj1.getString());
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList of objects where each object contains a string 'word' and
I have static ArrayList<Batch> . i want to remove which object contain productcode This
I have an ArrayList which contains data like profId, firstname, lastname, age. Now I
I have a two-dimension ArrayList that contains double values: ArrayList<ArrayList<Double>> data = new ArrayList<ArrayList<Double>>();
Possible Duplicate: How to compare two arraylist? I have two String ArrayLists of different
I have a class that contains two arraylists which I'm trying to store objects
I have an ArrayList of objects of my custom class. I would like to
I have a problem. I have an ArrayList of objects. Each object contains a
I have a couple of ArrayLists with variable length and sometimes null. This ArrayList
I have a class called Paragens like this: public class Paragens { static int

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.