如何按日期对对象列表进行排序(java 集合,List

发布于 2024-10-20 05:44:42 字数 1951 浏览 5 评论 0原文

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) 处 entitybeans.jsf.PeliculaController$1.compare(PeliculaController.java:260) 在 java.util.Arrays.mergeSort(Arrays.java:1270) 处 java.util.Arrays.sort(Arrays.java:1210) 在 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) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 处 javax.el.BeanELResolver.getValue(BeanELResolver.java:302) 在 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) 处 com.sun.el.parser.AstValue.getValue(AstValue.java:163)....

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

死开点丶别碍眼 2024-10-27 05:44:42

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

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());
    }
});
网白 2024-10-27 05:44:42

不要访问或修改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();

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();
心头的小情儿 2024-10-27 05:44:42

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

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

In Java 8+, it's now as simple as:

movieItems.sort(Comparator.comparing(Movie::getDate));
神经暖 2024-10-27 05:44:42

您错误地使用了比较器

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

You're using Comparators incorrectly.

 Collections.sort(movieItems, new Comparator<Movie>(){
           public int compare (Movie m1, Movie m2){
               return m1.getDate().compareTo(m2.getDate());
           }
       });
猛虎独行 2024-10-27 05:44:42

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

I'd add Commons NullComparator instead to avoid some problems...

萌能量女王 2024-10-27 05:44:42

你可以使用这个:

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

You can use this:

Collections.sort(list, org.joda.time.DateTimeComparator.getInstance());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文