This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to sort array list wich is contain diffrent objects

 
kavita vala
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a arraylist which contains objects
Like



In serv object i have different listing fields like name , email, postedate etc.
I have to sort this arraylist by postedate .

I do not want to write it in query because on later stage i divide this arraylist in two
different category.

How can i do it.


Please help for that
Thanks in advance .
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the method Collections.sort() that takes a List and a Comparator.

The Comparator is the thing that determines how the objects are sorted; implement your own Comparator that compares the objects by the fields that you want to sort on.
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the contents of your List are not appropriate for the Comparator<T>, you will suffer a ClassCastException. That is why you ought to be using the generic version of the List.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic