简体   繁体   中英

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. 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

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

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

Collections.sort(list, myComparator);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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