4

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
kozla13
  • 1,854
  • 3
  • 23
  • 35

2 Answers2

19

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());
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

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