Lukas Anda
Lukas Anda

Reputation: 746

Sort a list of JodaTime dates by collections.sort

I am using this algorithm to sort the list of dates in ascending order:

Collections.sort(eventList, new Comparator<Event>() {
        @Override
        public int compare(Event e1, Event e2) {
            DateTime dt1 = new DateTime(e1.getStartDate());
            DateTime dt2 = new DateTime(e2.getStartDate());
            return dt1.isBefore(dt2)? 1:0;
        }
    });
    Event first = eventList.get(0);

My question is, is this the best and correct way of doing things ?

This is the second suggestion:

Collections.sort(eventList, new Comparator<Event>() {
        @Override
        public int compare(Event e1, Event e2) {
            DateTime dt1 = new DateTime(e1.getStartDate());
            DateTime dt2 = new DateTime(e2.getStartDate());
            if(dt1.isBefore(dt2))
                return -1;
            else if(dt1.isAfter(dt2))
                return 1;
            else return 0;
        }
    });
    Event first = eventList.get(0);

Upvotes: 0

Views: 1017

Answers (2)

user7605325
user7605325

Reputation:

There are 2 alternatives.

1- use a Comparator that compares the DateTime for each Event:

Collections.sort(eventList, new Comparator<Event>() {
    @Override
    public int compare(Event event1, Event event2) {
        DateTime d1 = new DateTime(event1.getStartDate());
        DateTime d2 = new DateTime(event2.getStartDate());
        return d1.compareTo(d2);
    }
});

Actually, if getStartDate() returns a java.util.Date, you don't need to create a DateTime because java.util.Date also implements Comparable. In this case, you could just do:

Collections.sort(eventList, new Comparator<Event>() {
    @Override
    public int compare(Event event1, Event event2) {
        return event1.getStartDate().compareTo(event2.getStartDate());
    }
});

2- make Event implements Comparable:

public class Event implements Comparable<Event> {

    @Override
    public int compareTo(Event other) {
        DateTime dt1 = new DateTime(this.getStartDate());
        DateTime dt2 = new DateTime(other.getStartDate());
        return dt1.compareTo(dt2);
    }
}

Collections.sort(eventList);

In this case, Collections.sort will use Event.compareTo method to sort.

Again, if getStartDate() returns a java.util.Date, you just need to do:

public class Event implements Comparable<Event> {

    @Override
    public int compareTo(Event other) {
        return this.getStartDate().compareTo(other.getStartDate());
    }
}

Which one to choose? It depends.

If you use alternative 2 (implementing Comparable), it means that comparing the start date is the "natural order" of Event objects (the "normal"/"usual" way of sorting events).

Using a Comparator (in my opinion) is an alternative when you want to use a different criteria (other than the "natural order") to sort.

But it's up to you to choose.

Upvotes: 2

Edson Menegatti
Edson Menegatti

Reputation: 4036

Since DateTime already implements the Comparable interface, you could do:

Collections.sort(eventList, new Comparator<Event>() {
    @Override
    public int compare(Event e1, Event e2) {
        DateTime dt1 = new DateTime(e1.getStartDate());
        DateTime dt2 = new DateTime(e2.getStartDate());
        return dt1.compareTo(dt2);
    }
});

Upvotes: 1

Related Questions