I'm sorry if this is a duplicate, but I couldn't find a solution in posts I read.
I have a custom class, that contains few Strings and a Date. I made and ArrayList of this structure, and now, I want this ArrayList to be sorted from newest to oldest, using the Date field. How can I sort this list? Is there any way to build a custom comparator, that will sort ArrayList according to date in my class?
My class code:
public class ParsedWebData {
String title;
String url;
String description;
Date date;
String dateString;
public ParsedWebData() {
}
public ParsedWebData(String title, String url, String description, Date date, String dateString) {
this.title = title;
this.url = url;
this.description = description;
this.date = date;
this.dateString = dateString;
}
public String getTitle() {
return title;
}
public String getUrl() {
return url;
}
public String getDescription() {
return description;
}
public Date getDate() {return date;}
public String getDateString() {return dateString;}
}
So my arraylist declaration looks like that:
ArrayList<ParsedWebData> data = new ArrayList<>;
EDIT 1
I tried to use :
list.sort(Comparator.comparing(ParsedWebData::getDate));
But I get:
Call requires API level 24 (current min is 16): java.util.ArrayList#Sort