【发布时间】:2017-01-10 21:01:36
【问题描述】:
我正在尝试这样做:按今天、本周、本月和今年对对象值数组(日期类型)进行排序,并且我知道如何使用 Comparator 类按降序或升序对日期数组进行排序,但我不知道如何像我说的那样对数组进行排序,到今天、本周、本月或今年。
private void sortTopicsByDate() {
Collections.sort(topics, new Comparator<Topic>() {
@Override
public int compare(Topic o1, Topic o2) {
return o1.getCreatedTime().compareTo(o2.getCreatedTime());
}
});
}
更新(包含今天创建的照片的过滤列表)
private List<Topic> getFilteredTopics() {
List<Topic> filteredList = new ArrayList<>();
Date now = new Date(); // today date
Calendar cal = Calendar.getInstance();
Calendar getCal = Calendar.getInstance();
cal.setTime(now);
int nYear = cal.get(Calendar.YEAR);
int nMonth = cal.get(Calendar.MONTH);
int nDay = cal.get(Calendar.DAY_OF_MONTH);
if (topics != null) {
for (Topic topic : topics) {
getCal.setTime(topic.getCreatedTime());
int year = getCal.get(Calendar.YEAR);
int month = getCal.get(Calendar.MONTH);
int day = getCal.get(Calendar.DAY_OF_MONTH);
if (nDay == day && month == nMonth) {
filteredList.add(topic);
}
}
}
return filteredList;
}
【问题讨论】:
-
什么是“这个”?
-
显示你到目前为止尝试过的内容
-
“按今天、本周、本月或今年排序”是什么意思?你能在你的问题中添加一个具体的例子吗?
-
你当前的代码有什么问题?
-
你的意思是你想
filter你的照片?
标签: java android arrays sorting