1

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

2
  • 2
    you can query by date.try this sql "SELECT s FROM SuggestionEntity s order by s.suggestionDate desc".the "s.suggestionDate" is your database field .than the result was sort already. Commented Oct 12, 2013 at 8:38
  • stackoverflow.com/questions/2784514/… Commented Oct 12, 2013 at 8:41

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks alot!! But this is applicable to java ejb query as well right?
Ofcourse, It supports sql right ? Syntax might change. But concept is same. :)
Thanks a lot for the help! Appreciate it :)
0

You should write your own comparator, and then sort it like following:

Collections.sort(list, myComparator);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.