Reputation: 241
I have a list of ArrayList, and each arraylist consists of a date and a string. I want to sort the list according to the date in the arraylist. I tried looking for answers but cant find it online. Most of the examples are sorting a list of objects by date. Any suggestions?
public List<ArrayList> SuggestionReport() {
Query q = em.createQuery("SELECT s FROM SuggestionEntity s");
List<ArrayList> report = new ArrayList();
for (Object o : q.getResultList()) {
suggestionEntity = (SuggestionEntity) o;
ArrayList suggestion = new ArrayList();
suggestion.add(suggestionEntity.getSuggestionDate());
suggestion.add(suggestionEntity.getContent());
report.add(suggestion);
}
return report;
}
ps. I want to sort the list before returning the list
Upvotes: 1
Views: 386
Reputation: 121998
Need not to write extra code, Just change your query to
SELECT s FROM SuggestionEntity s order by suggestion_date ASC|DESC
Where suggestion_date
is your column name.
ASC|DESC
is order you want, choose ASC
or DESC
based on your requirment.
Learn more here about order by
Upvotes: 4
Reputation: 31
You should write your own comparator, and then sort it like following:
Collections.sort(list, myComparator);
Upvotes: 0