Java,使用比较器对ArrayList进行排序

时间:2014-10-23 14:52:20

标签: java sorting comparator

我想按两个标准对Task个对象列表进行排序。首先,它按leftEntryTime排序,然后按timeNeededToBeProcessed排序。

有人可以帮助我吗?

到目前为止,我按leftEntryTime进行了排序,但我如何在同一timeNeededToBeProcessed类中按Comparator对其进行排序?

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

这是我的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;
    }
}

然后我按它排序:

Collections.sort(list, new TimeEntryComparator());

5 个答案:

答案 0 :(得分:2)

类似的东西:

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();
    }
}

答案 1 :(得分:2)

使用一些新的Java 8功能,它实际上非常简单和优雅:

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));

答案 2 :(得分:1)

我相信你必须这样做:

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

这会将Task个对象与LeftEntryTime进行比较,如果leftEntryTime属性相等,则会比较他们的timeNeededToBeProcessed属性。

答案 3 :(得分:0)

当leftEntryTime值的比较给出0(即它们相等)时,继续按timeNeededToBeProcessed进行比较,例如:

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

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

答案 4 :(得分:0)

我认为您是说您希望通过由TaskleftEntryTime字段组成的复合键对timeNeededToBeProcessed个对象进行排序。表示此类排序的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;
        } 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;
            }
        }
    }
}