kozla13
kozla13

Reputation: 1942

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

Upvotes: 4

Views: 4812

Answers (2)

Jens Birger Hahn
Jens Birger Hahn

Reputation: 917

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

Upvotes: -1

Jon Skeet
Jon Skeet

Reputation: 1502256

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

Upvotes: 19

Related Questions