Win a copy of Mesos in Action this week in the Cloud/Virtualizaton 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: 15279
39
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
Pie
Posts: 48940
60
  • 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