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
  • 262,936
  • 84
  • 758
  • 1,028
kozla13
  • 1,773
  • 3
  • 21
  • 34

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,335,956
  • 823
  • 8,931
  • 9,049
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(..).