如何按日期对对象列表进行排序(java 集合,List
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在您的
compare
方法中,o1
和o2
已经是movieItems
列表中的元素。所以,你应该这样做:In your
compare
method,o1
ando2
are already elements in themovieItems
list. So, you should do something like this:不要访问或修改
Comparator
中的集合。比较器应该仅用于确定哪个对象位于另一个对象之前。要比较的两个对象作为参数提供。Date
本身是可比较的,因此,使用泛型:并且不要在每次排序时实例化比较器。使用:
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:And do not instantiate the comparator on each sort. Use:
在 Java 8+ 中,现在很简单:
In Java 8+, it's now as simple as:
您错误地使用了
比较器
。You're using
Comparators
incorrectly.我会添加 Commons NullComparator 来避免一些问题......
I'd add Commons NullComparator instead to avoid some problems...
你可以使用这个:
You can use this: