我尝试使用以下代码根据日期顺序对对象的ArrayList进行排序:
List<GalleryBean> gallery_beans = this.galleryAdapter.getGalleryImageList();
Collections.sort(gallery_beans, new Comparator<GalleryBean>() {
public int compare(GalleryBean bean1, GalleryBean bean2) {
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;
/*
if(bean1.getMediaDate() > bean2.getMediaDate()){
Log.i("Collections.sort---->", "move bean1 name, long, date ---> ," +
bean1.getMediaName() + ", " + bean1.getMediaDate() + ", " + new Date(bean1.getMediaDate()) +
", AFTER bean2 name, long, date ---> "+ bean2.getMediaName() + ", " + bean2.getMediaDate() + ", " + new Date(bean2.getMediaDate()));
return AFTER;
} else if(bean1.getMediaDate() < bean2.getMediaDate()){
Log.i("Collections.sort---->", "move bean1 name, long, date ---> ," +
bean1.getMediaName() + ", " + bean1.getMediaDate() + ", " + new Date(bean1.getMediaDate()) +
", BEFORE bean2 name, long, date ---> "+ bean2.getMediaName() + ", " + bean2.getMediaDate() + ", " + new Date(bean2.getMediaDate()));
return BEFORE;
} else {
return EQUAL;
}
*/
return (int) (bean1.getMediaDate() - bean2.getMediaDate());
}
});
for (Iterator<GalleryBean> it = gallery_beans.iterator(); it.hasNext(); i++) {
GalleryBean gallery_bean = it.next();
Log.i("ITERATOR---->", "gallery_bean name ---> " + gallery_bean.getMediaName() + " gallery_bean long date ---> " + gallery_bean.getMediaDate() + " gallery_bean date ---> " + new Date(gallery_bean.getMediaDate()*1000));
}
从我的研究中,注释掉的代码应该可以工作,但似乎什么都不做。剩下的行做了一些排序,但它也不正确。这是下面的return(int)*代码的日志输出片段。我试图基于长整数格式的日期进行排序。非常感谢任何帮助。
此致 JT_Dylan
11-24 21:03:47.407: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_140338.jpg gallery_bean long date ---> 1408125820000 gallery_bean date ---> Sat Sep 24 08:06:40 EST 46591
11-24 21:03:47.407: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_140408.jpg gallery_bean long date ---> 1408125850000 gallery_bean date ---> Sat Sep 24 16:26:40 EST 46591
11-24 21:03:47.407: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_152133.jpg gallery_bean long date ---> 1408130495000 gallery_bean date ---> Thu Nov 17 11:43:20 EDT 46591
11-24 21:03:47.407: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_152134.jpg gallery_bean long date ---> 1408130496000 gallery_bean date ---> Thu Nov 17 12:00:00 EDT 46591
11-24 21:03:47.407: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_152137.jpg gallery_bean long date ---> 1408130498000 gallery_bean date ---> Thu Nov 17 12:33:20 EDT 46591
11-24 21:03:47.409: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_152140.jpg gallery_bean long date ---> 1408130501000 gallery_bean date ---> Thu Nov 17 13:23:20 EDT 46591
11-24 21:03:47.409: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_152231.jpg gallery_bean long date ---> 1408130553000 gallery_bean date ---> Fri Nov 18 03:50:00 EDT 46591
11-24 21:03:47.409: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_153950.jpg gallery_bean long date ---> 1408131592000 gallery_bean date ---> Wed Nov 30 04:26:40 EDT 46591
11-24 21:03:47.411: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_154004.jpg gallery_bean long date ---> 1408131605000 gallery_bean date ---> Wed Nov 30 08:03:20 EDT 46591
11-24 21:03:47.411: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_154011.jpg gallery_bean long date ---> 1408131613000 gallery_bean date ---> Wed Nov 30 10:16:40 EDT 46591
11-24 21:03:47.411: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_154011_1.jpg gallery_bean long date ---> 1408131613000 gallery_bean date ---> Wed Nov 30 10:16:40 EDT 46591
11-24 21:03:47.414: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_162439.jpg gallery_bean long date ---> 1408134281000 gallery_bean date ---> Sat Dec 31 07:23:20 EDT 46591
11-24 21:03:47.414: I/ITERATOR---->(3949): gallery_bean name ---> IMG_20140815_162446.jpg gallery_bean long date ---> 1408134288000 gallery_bean date ---> Sat Dec 31 09:20:00 EDT 46591
答案 0 :(得分:0)
我假设getMediaDate()
的结果是long
值。在比较值时,您将向下转换为int
,这会从长整数中截断位并产生意外结果。尝试这样的事情......
public int compare(GalleryBean bean1, GalleryBean bean2) {
long value = bean2.getMediaDate() - bean1.getMediaDate();
return value == 0 ? 0 : value > 0 ? 1 : -1;
}
无论长值有多大差异,我们都会返回int
-1
,0
或1
。
对于降序我们在减法时反转交换豆...
long value = bean1.getMediaDate() - bean2.getMediaDate();
答案 1 :(得分:0)
您还需要处理其中一个为null的实例。例如,如果NULL小于实际值,那么您的检查可能是:
public int compare(GalleryBean bean1, GalleryBean bean2) {
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;
int result = EQUAL;
if (bean1 == null){
if (bean2 != null){
result = BEFORE;
}
} else {
if (bean2 == null){
result = AFTER;
} else {
if(bean1.getMediaDate() > bean2.getMediaDate()){
result = AFTER;
} else if(bean1.getMediaDate() < bean2.getMediaDate()){
result = BEFORE;
}
}
}
return result;
}