Sort Intervals in Joda-Time

后端 未结 2 1904
鱼传尺愫
鱼传尺愫 2020-12-19 17:01

I have list of Joda-Time Interval objects.

List intervals = new ArrayList();

How can I sort the intervals o

相关标签:
2条回答
  • 2020-12-19 17:02

    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());
    
    0 讨论(0)
  • 2020-12-19 17:16

    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(..).

    0 讨论(0)
提交回复
热议问题