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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:41:48+00:00 2026-05-31T16:41:48+00:00

I have a arraylist of a POJO and the data in it is of

  • 0

I have a arraylist of a POJO and the data in it is of the form

id    time
2     467
3     403
4     602
3     529
5     398

The requirement is that first I need to sort the data by time and then after that the same IDs should be one after the other i.e

id     time
5      398
3      403
3      529
2      467
4      602.

Initially to sort by time , I’m using the following logic

Collections.sort(list, new Comparator<Asset>() {
                    @Override
                    public int compare(Asset o1, Asset o2) {

                        if (o1.getTime() > o2.getTime())

                            return -1;

                        else if (o1.getTime() < o2.getTime())

                            return 1;

                        else

                            return 0;

                    }

                });

Could some one help me in clubbing by IDs in the next stage?

  • 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-31T16:41:49+00:00Added an answer on May 31, 2026 at 4:41 pm

    To sort the data according to the example you gave, you probably need two passes over the list. (How else would you determine whether or not 3 504 should come before or after 5 315?)

    1. Sort according to time.
    2. Sort the list according to the first index of each id.

    Here’s some sample code:

    import java.util.*;
    
    class Asset {
        public int id;
        public int time;
    
        public Asset(int id, int time) {
            this.id = id;
            this.time = time;
        }
    
        public String toString() {
            return id + "  " + time;
        }
    }
    
    
    class Test {
        public static void main(String[] args) {
    
            List<Asset> assets = new ArrayList<Asset>();
            assets.add(new Asset(2, 467));
            assets.add(new Asset(3, 403));
            assets.add(new Asset(4, 602));
            assets.add(new Asset(3, 529));
            assets.add(new Asset(5, 398));
    
            // Sort according to time.
            Collections.sort(assets, new Comparator<Asset>() {
                @Override
                public int compare(Asset o1, Asset o2) {
                    return new Integer(o1.time).compareTo(o2.time);
                }
            });
    
            // Remember the original indexes of each asset.
            final List<Asset> assetsCopy = new ArrayList<Asset>(assets);
    
            // Sort the collection based on the index of the first asset
            // with the same id
            Collections.sort(assets, new Comparator<Asset>() {
    
                private int firstIndexOf(int id) {
                    for (int i = 0; i < assetsCopy.size(); i++)
                        if (assetsCopy.get(i).id == id)
                            return i;
                    return -1;
                }
    
                @Override
                public int compare(Asset o1, Asset o2) {
                    return new Integer(firstIndexOf(o1.id))
                            .compareTo(firstIndexOf(o2.id));
                }
            });
    
    
            for (Asset a : assets)
                System.out.println(a);
        }
    }
    

    Output:

    5  398
    3  403
    3  529
    2  467
    4  602
    
    • 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 that I need to sort in two different
I have an arraylist that contains urls in the form similar to : stackoverflow.com/questions/ask/hello
I have an arraylist that gets different type of values in it, 1st value->
I have an ArrayList in my Servlet code. Now, I want to show that
I have an ArrayList<MyObject> that may (or may not) contain duplicates of MyObject I
I have an ArrayList of Objects(POJOs) that have an Id and another field. I
I have this arrayList: ArrayList<ArrayList<Integer>> temporary = new ArrayList<ArrayList<Integer>>(); Some how I need to
I have an ArrayList which contains data like profId, firstname, lastname, age. Now I
I have a json object that is actually an arraylist of objects previously converted
suppose I have arraylist of integers...is there a way that I can generate a

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.