[英]Sorting a list of ArrayList by date in the ArrayList
I have a list of ArrayList, and each arraylist consists of a date and a string. 我有一个ArrayList列表,每个arraylist包含一个日期和一个字符串。 I want to sort the list according to the date in the arraylist. 我想根据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. PS。 I want to sort the list before returning the list 我想在返回列表之前对列表进行排序
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. 其中suggestion_date
是您的列名。
ASC|DESC
is order you want, choose ASC
or DESC
based on your requirment. ASC|DESC
是您想要的订单,根据您的要求选择ASC
或DESC
。
您应该编写自己的比较器,然后按如下方式对其进行排序:
Collections.sort(list, myComparator);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.