Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort Intervals in Joda-Time

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

like image 713
kozla13 Avatar asked Dec 01 '22 21:12

kozla13


1 Answers

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());
like image 52
Jon Skeet Avatar answered Dec 05 '22 20:12

Jon Skeet