0

I want to sort my list of Task object by two criterion. First it is sorted by leftEntryTime and then by timeNeededToBeProcessed.

Can someone help me?

So far I did sorting by leftEntryTime but how I can sort it by timeNeededToBeProcessed in the same Comparator class?

 Task(int procesID, int neededTime, int waitingTime, int leftEntryTime) {
    this.procesID = procesID;
    this.timeNeededToBeProcessed = neededTime;
    this.waitingTime = waitingTime;
    this.leftEntryTime = leftEntryTime;
 }

This is my Comparator:

import java.util.Comparator;

public class TimeEntryComparator implements Comparator<Task> {

    @Override
    public int compare(Task o1, Task o2) {
        int entryTime;
        int taskTime = o1.getLeftEntryTime()- o2.getLeftEntryTime();

        if (taskTime > 0){
            return 1;
        }

        if (taskTime < 0) {
            return -1;
        }
        return 0;
    }
}

then I sort it by:

Collections.sort(list, new TimeEntryComparator());
3
  • add one more if? what is the issue? are you getting unexpected results? please share them Commented Oct 23, 2014 at 14:57
  • let me shorten your code: public int compare(Task o1, Task o2) { return (o1.getLeftEntryTime() - o2.getLeftEntryTime()); } Commented Oct 23, 2014 at 14:58
  • What is the unit of time stored in leftEntryTime and timeNeededToBeProcessed ? Commented Oct 23, 2014 at 15:00

5 Answers 5

2

something like:

public class TimeEntryComparator implements Comparator<Task> {
    @Override
    public int compare(Task o1, Task o2) {
        if (o1.getLeftEntryTime() == o2.getLeftEntryTime())
            return o1.getTimeNeededToBeProcessed() - o2.getTimeNeededToBeProcessed();
        return o1.getLeftEntryTime() - o2.getLeftEntryTime();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Using some new Java 8 functionality, it's actually pretty easy and elegant:

Comparator<Task> byLeftEntryTime = (t1, t2) -> Integer.compare(t1.getLeftEntryTime(), t2.getLeftEntryTime());
Comparator<Task> byTimeNeededToBeProcessed = (t1, t2) -> Integer.compare(t1.getTimeNeededToBeProcessed(), t2.getTimeNeededToBeProcessed());

list.sort(byLeftEntryTime.thenComparing(byTimeNeededToBeProcessed));

Comments

1

I believe you have to do:

@Override
public int compare(Task o1, Task o2) {
    if (o1.getLeftEntryTime() == o2.getLeftEntryTime()) {
        return o1.getTimeNeededToBeProcessed - o2.getTimeNeededToBeProcessed();
    } else {
        return o1.getLeftEntryTime() - o2.getLeftEntryTime();
    }
}

This will compare the Task objects by LeftEntryTime and if the leftEntryTime properties are equal, it will compare their timeNeededToBeProcessed properties.

Comments

0

When the comparison of the leftEntryTime values gives 0 (i.e. they are equal), then continue to compare by timeNeededToBeProcessed, e.g.:

@Override
public int compare(Task o1, Task o2) {
    int compare = o1.getLeftEntryTime()- o2.getLeftEntryTime();
    if(compare != 0) {
        return compare;
    }

    return o1.getTimeNeededToBeProcessed() - o2.getTimeNeededToBeProcessed();
}

Comments

0

I think you're saying that you want to sort your Task objects by a compound key consisting of their leftEntryTime and timeNeededToBeProcessed fields. A Comparator that represents such an ordering would look like this:

import java.util.Comparator;

public class TimeEntryComparator implements Comparator<Task> {

    @Override
    public int compare(Task o1, Task o2) {
        int entryTime;
        int taskTime = o1.getLeftEntryTime()- o2.getLeftEntryTime();

        if (taskTime > 0){
            return 1;
        } else if (taskTime < 0) {
            return -1;
        } else {
            int neededTime = o1.getTimeNeededToBeProcessed()
                    - o2.getTimeNeededToBeProcessed();

            if (neededTime > 0) {
                return 1;
            } else if (neededTime < 0) {
                return -1;
            } else {
                return 0;
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.