Every example I find is about doing this alphabetically, while I need my elements sorted by date.
My ArrayList contains objects on which one of the datamembers is a
The Date class already implements Comparator interface. Assuming you have the class below:
public class A {
private Date dateTime;
public Date getDateTime() {
return dateTime;
}
.... other variables
}
And let's say you have a list of A objects as List aList
, you can easily sort it with Java 8's stream API (snippet below):
import java.util.Comparator;
import java.util.stream.Collectors;
...
aList = aList.stream()
.sorted(Comparator.comparing(A::getDateTime))
.collect(Collectors.toList())