Sort Intervals in Joda-Time

旧时模样 提交于 2019-11-28 04:54:41

问题


I have list of Joda-Time Interval objects.

List<Interval> intervals = new ArrayList<Interval>();

How can I sort the intervals on the beginning Date of each interval. The intervals are not overlapping


回答1:


Just create a Comparator<Interval> which compares by start times:

public class IntervalStartComparator implements Comparator<Interval> {
    @Override
    public int compare(Interval x, Interval y) {
        return x.getStart().compareTo(y.getStart());
    }
}

Then sort using that:

Collections.sort(intervals, new IntervalStartComparator());



回答2:


In your special case, collect the start instants using

interval.getStart()

in another list. DateTime using the Comparable interface which makes the list sortable using Collections.sort(..).



来源:https://stackoverflow.com/questions/16986888/sort-intervals-in-joda-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!