如何按对象的日期对对象列表进行排序(java集合,List< Object>) [英] How to sort a List of objects by their date (java collections, List<Object>)

查看:233
本文介绍了如何按对象的日期对对象列表进行排序(java集合,List< Object>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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()});
Collection.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
at 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)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl。 java:25)
at java.lang.reflect.Method.invoke(Method.java:597)at
javax.el.BeanELResolver.getValue(BeanELResolver.java:302)at
javax .el.CompositeELResolver.getValue(CompositeELResolver.java:175)
at
com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
at com.sun.el .parser.AstValue.getValue(AstValue.java:116)at
com.sun.el.parser.AstValue.getValue(AstValue.java:163)....


比较方法中, o1

code>和 o2 已经是 movieItems 列表中的元素。所以,你应该这样做:

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


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 is bringing back 4 movies and is giving me the following

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 4 at java.util.Vector.get(Vector.java:694) at entitybeans.jsf.PeliculaController$1.compare(PeliculaController.java:260) at 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at javax.el.BeanELResolver.getValue(BeanELResolver.java:302) at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175) at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72) at com.sun.el.parser.AstValue.getValue(AstValue.java:116) at com.sun.el.parser.AstValue.getValue(AstValue.java:163)....

解决方案

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

这篇关于如何按对象的日期对对象列表进行排序(java集合,List&lt; Object&gt;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆