如何按日期(java集合,List <object>)</object>对对象列表进行排序

时间:2011-03-05 21:53:42

标签: java sorting collections

private List<Movie> movieItems = null;
public List<Movie> getMovieItems() {
    final int first = 0;
    if (movieItems == null) {
        getPagingInfo();
        movieItems = jpaController.findRange(new int[]{pagingInfo.getFirstItem(), pagingInfo.getFirstItem() + pagingInfo.getBatchSize()});
        Collections.sort(movieItems, new Comparator(){
           public int compare (Object o1, Object o2){
               Date d1 = movieItems.get(((Movie)o1).getMovieId()).getDate();
               Date d2 = movieItems.get(((Movie)o2).getMovieId()).getDate();
               if(d1.before(d2)){
                   movieItems.set(1, (Movie)o1);
                   movieItems.set(2, (Movie)o2);
               }
               return first;
           }
       });
    }
    return movieItems;
}

jpaController带回了4部电影,并给了我以下

  

java.lang.ArrayIndexOutOfBoundsException:数组索引超出范围:4     在java.util.Vector.get(Vector.java:694)at   entitybeans.jsf.PeliculaController $ 1.compare(PeliculaController.java:260)     在java.util.Arrays.mergeSort(Arrays.java:1270)at   java.util.Arrays.sort(Arrays.java:1210)at   java.util.Collections.sort(Collections.java:159)at   entitybeans.jsf.PeliculaController.getPeliculaItems(PeliculaController.java:257)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)at   javax.el.BeanELResolver.getValue(BeanELResolver.java:302)at at   javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)     在   com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)     在com.sun.el.parser.AstValue.getValue(AstValue.java:116)at   com.sun.el.parser.AstValue.getValue(AstValue.java:163)......

7 个答案:

答案 0 :(得分:53)

compare方法中,o1o2已经是movieItems列表中的元素。所以,你应该做这样的事情:

Collections.sort(movieItems, new Comparator<Movie>() {
    public int compare(Movie m1, Movie m2) {
        return m1.getDate().compareTo(m2.getDate());
    }
});

答案 1 :(得分:19)

请勿访问或修改Comparator中的集合。比较器应仅用于确定哪个对象先于另一个对象。要比较的两个对象作为参数提供。

Date本身具有可比性,因此,使用泛型:

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

并且不要在每种类型上实例化比较器。使用:

private static final MovieComparator comparator = new MovieComparator();

答案 2 :(得分:16)

在Java 8中,它现在很简单:

movieItems.sort(Comparator.comparing(Movie::getDate));

答案 3 :(得分:14)

您错误地使用了Comparators

 Collections.sort(movieItems, new Comparator<Movie>(){
           public int compare (Movie m1, Movie m2){
               return m1.getDate().compareTo(m2.getDate());
           }
       });

答案 4 :(得分:1)

我会添加Commons NullComparator来避免一些问题......

答案 5 :(得分:0)

您可以使用:

Collections.sort(list, org.joda.time.DateTimeComparator.getInstance());

答案 6 :(得分:0)

使用Java 8功能将更加简单。

语法: LIST_NAME.sort(Comparator.comparing(INFO_CLASS :: getMethod));

示例: movieItems.sort(Comparator.comparing(Movie :: getDate));