private List movieItems = null;
public List getMovieItems() {
final int first = 0;
if (movieItems == null) {
getPagingInfo(
Do not access or modify the collection in the Comparator
. The comparator should be used only to determine which object is comes before another. The two objects that are to be compared are supplied as arguments.
Date
itself is comparable, so, using generics:
class MovieComparator implements Comparator<Movie> {
public int compare(Movie m1, Movie m2) {
//possibly check for nulls to avoid NullPointerException
return m1.getDate().compareTo(m2.getDate());
}
}
And do not instantiate the comparator on each sort. Use:
private static final MovieComparator comparator = new MovieComparator();
You can use this:
Collections.sort(list, org.joda.time.DateTimeComparator.getInstance());
You're using Comparators
incorrectly.
Collections.sort(movieItems, new Comparator<Movie>(){
public int compare (Movie m1, Movie m2){
return m1.getDate().compareTo(m2.getDate());
}
});
In your compare
method, o1
and o2
are already elements in the movieItems
list. So, you should do something like this:
Collections.sort(movieItems, new Comparator<Movie>() {
public int compare(Movie m1, Movie m2) {
return m1.getDate().compareTo(m2.getDate());
}
});
In Java 8, it's now as simple as:
movieItems.sort(Comparator.comparing(Movie::getDate));
I'd add Commons NullComparator instead to avoid some problems...