简体   繁体   English

在ArrayList中按日期排序ArrayList列表

[英]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是您想要的订单,根据您的要求选择ASCDESC

Learn more here about order by 点击此处了解更多订单

您应该编写自己的比较器,然后按如下方式对其进行排序:

Collections.sort(list, myComparator);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM