aspose file tools*
The moose likes Java in General and the fly likes How to sort array list wich is contain diffrent objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to sort array list wich is contain diffrent objects" Watch "How to sort array list wich is contain diffrent objects" New topic
Author

How to sort array list wich is contain diffrent objects

kavita vala
Ranch Hand

Joined: Jul 22, 2010
Posts: 42
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

Joined: Aug 16, 2005
Posts: 14347
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to sort array list wich is contain diffrent objects