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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:20:10+00:00 2026-05-28T13:20:10+00:00

Class Activity{ private int level; private Activity predecessor; } My program creates instances of

  • 0
Class Activity{
    private int level;
    private Activity predecessor;
}

My program creates instances of Activity, puts them in an Arraylist and sorts them by two fields level and predecessor. predecessor is first compared.

For instance, if predecessor is not null, the predecessor Activity of the current activity should be first and it will be second. After satisfying the predecessor condition the following could be ordered by level.

List<Activity> activities = new ArrayList<Activity>();
//Add some activities.
activities.add(Activity);
activities.add(Activity);
Collections.sort(activities,getActivityComparator());

private Comparator<Activity> getActivityComparator() {
    return new Comparator<Activity>() { 
        public int compare(Activity act1, Activity act2) {                
            if (act1 == null) {
                if (act2 == null) {
                    return 0; // Both activities are null
                } else {
                    return -1; // act1 is NULL, so put act1 in the end of
                    // the sorted list
                }
            } else {
                if (act2 == null) {
                    return 1; 
                }
            }

         Activity preAct2 = act2.getPredecessor();
         if(preAct2!=null&&preAct2==act1)
             return -1;

          //Adding this by Joeri Hendrickx's suggestion
         Activity preAct1 = act1.getPredecessor();
         if(preAct1!=null&&preAct1==act2)
             return 1;

         return act2.getLevel()-act1.getLevel(); 
        }
    };
}

I have written the above code and tested a lot, but it outputted inconstant ordering if I putted different ordering into Arrarylist. so definitely this is incorrect method to implement this. Seems the root cause is not every two elements were compared. says Activity act1>Activity act2(by level condition), Activity act2>Activity act3(by level condition), it doesn’t mean Activity act1>Activity act3(by predecessor condition).

Here I came up with an new idea that is to use bubble sort instead of using interface sort of Collections. it will compare every two elements that don’t need comparator transitive any more. how do you think?

Can anyone help me?

  • 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-28T13:20:11+00:00Added an answer on May 28, 2026 at 1:20 pm

    The problem is that your comparator is not describing a correct symmetric and transitive relation.

    Consider the following three objects:

    act1{predecessor:null, level:1}    
    act2{predecessor:act1, level:1}    
    act3{predecessor:null, level:0}
    act4{predecessor:act2, level:2}
    

    Now, where comp is your comparator, comp(act1, act2) will return -1, but comp(act2, act1) will return 0. This is not symmetric.

    comp(act1, act2) will return -1, comp(act2, act4) will return -1, but comp(act1, act4) will return 1. This is not transitive.

    A comparator must describe a correct reflexive, symmetric and transitive relation in order for it to work.

    Edited after seeing Axtaxt’s post: the problem you describe here cannot be accomplished with a normal sort, because it’s always possible to construct a scenario where, by these rules, you get a loop in your sort hierarchy. So it’s not possible to make a transitive comparator with only this data.

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

Sidebar

Related Questions

public class WordDisplay extends Activity { private int level; private int group; private int
i have executed the following code public class Activity_Threads_Handler extends Activity { private int
public class XPBN extends Activity{ private Map _map; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
public class VideoDemo extends Activity { private VideoView video; private MediaController ctlr; File clip=new
Here's my current code: public class MallMapActivity extends Activity { private final static String
I have a list of objects called Activity: class Activity { public Date activityDate;
i am using a class which extends Handler class to update my activity UI.
I'm currently writing a class to handle all database-activity in my application, and so
I have 3 classes in my example: Class A, the main activity. Class A
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;

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.