Dusan Dimitrijevic Dusan Dimitrijevic - 1 year ago 68
Android Question

How to filter list of photos - android?

I'm trying to do this: Sort array of objects values (Date type) by today date, this week, this month and this year and i know how to sort array of dates in descending or ascending order using Comparator class, but i don't know how to sort array like i said, by today date, this week, this month or this year.

private void sortTopicsByDate() {
Collections.sort(topics, new Comparator<Topic>() {
public int compare(Topic o1, Topic o2) {
return o1.getCreatedTime().compareTo(o2.getCreatedTime());

UPDATE (Filtering list with photos which are created today)

private List<Topic> getFilteredTopics() {
List<Topic> filteredList = new ArrayList<>();
Date now = new Date(); // today date
Calendar cal = Calendar.getInstance();
Calendar getCal = Calendar.getInstance();
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) {
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) {
return filteredList;

Answer Source

With java 8 you can use the streaming-apito filter your topics by date. Note that this solution does not include start and finish in the filter, if you want that you have to modify the condition in the filter.

Collection<Topic> topics = ...;
Date start = ...;
Date finish = ...;
List<Topic> filteredTopics = topics.stream()
    .filter(t -> t.getCreatedTime().after(start) && t.getCreatedTime().before(finish))