Can someone help me with this? 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 DateTime object. On DateTime I can call the functions:
lt() // less-than
lteq() // less-than-or-equal-to
So to compare I could do something like:
if(myList.get(i).lt(myList.get(j))){
// ...
}
I don't really know what to do inside the if block. Any ideas?
DateTime.compare?
The best answer IMHO from Tunaki using Java 8 lambda
Since Java 8 the List interface provides the sort method. Combined with lambda expression the easiest solution would be
With introduction of Java 1.8, streams are very useful in solving this kind of problems:
All the answers here I found to be un-neccesarily complex for a simple problem (at least to an experienced java developer, which I am not). I had a similar problem and chanced upon this (and other) solutions, and though they provided a pointer, for a beginner I found as stated above. My solution, depends on where in the the Object your Date is, in this case, the date is the first element of the Object[] where dataVector is the ArrayList containing your Objects.