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

  • SEARCH
  • Home
  • 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 8799493

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:20:18+00:00 2026-06-14T00:20:18+00:00

I have a list of objects where each object has variables previous and next

  • 0

I have a list of objects where each object has variables previous and next ,they are of type String

List testList=new ArrayList();
CustomObject  o1=new CustomObject();
o1.setPrevious(null);
o1.setNext("a");   

CustomObject  o2=new CustomObject();
o2.setPrevious("a");
o2.setNext("b");

CustomObject o3=new CustomObject ();   
o3.setPrevious("b");
o1.setNext("END");


testList.add(o3); 
testList.add(o1);  
testList.add(o2);

But i need to get it in the order o1,o2,o3.I have written a comparator to do this but i am not getting it right

class OrderComparator implements Comparator{
public int compare(CustomObject  obj1, CustomObject  obj2) {

        if(obj1.getPrevious()==null)
            return -1;
        else if(obj2.getPrevious()==null)
            return 1;
        else if(obj1.getNext()!=null&&obj2.getPrevious()!=null&& obj1.getNext().equals(obj2.getPrevious()))
            return -1;
        else if(obj1.getNext()!=null&&obj2.getPrevious()!=null&&obj2.getNext().equals(obj1.getPrevious()))
            return 1;
        else if(obj1.getNext().equals("END"))
            return -1;
        else if(obj2.getNext().equals("END"))
            return 1;   

        else return 0;
    }

}
Collections.sort(testList,new OrderComparator());

  • 0 0 Answers
  • 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-14T00:20:19+00:00Added an answer on June 14, 2026 at 12:20 am

    Your compare method is not complete. Consider the following ordered list of objects:
    obj1, obj2, obj3, obj4, obj5, END
    If I pass obj1 and obj5 to your compare method, they would not be processed correctly. The reason is that given an object o, you are checking its previous, next, the previous of the previous of the previous object and the next of the next object.

    This problem can be solved using the following pseudo code.

    //Checking if o1 is greater
    Object o=o1.previous;
    while(true){
       if ( o == null ) then o2 is not less than o1;
       if(o.equals(o2)) then o2 is less than o1
       else o=o.previous;
    }
    

    Similarly, check if o2 is greater

    • 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 order objects. Each order object has 5 instance variables.
I have a list of objects. Each object has an integer quantity and a
I have a list of objects: [Object_1, Object_2, Object_3] Each object has an attribute:
Background I have a list . This list has many objects. Each object has
I have a list of objects and in each object they contain a list
I have a List with objects. Each object has an ID. I want to
I have a list of core data objects each has a longitude and latitude
Let's say, we have and list of objects in variable called articles, each object
I have two list of different objects : List<Report> List<Newsletter> each having a 'created
I have a list of Point objects, (each one with x,y properties) and would

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.