*
The moose likes Java in General and the fly likes Sort ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sort ArrayList" Watch "Sort ArrayList" New topic
Author

Sort ArrayList

Shrawan Bhageria
Ranch Hand

Joined: Jun 20, 2005
Posts: 52
Hi All,

How we sort ArrayList.
If we sorre 20 car object in a ArrayList and we want to sort care object according to speed.How we can do that.

Regards,

Shrawan
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Have you checked out the API for the Collections class? There are some useful methods there.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
You can use Collections.sort() to sort an ArrayList.
You will either need your class to implement the Comparable interface and provide a suitable compareTo method, or you will need to create a Comparator class that implements a suitable compare method and pass this to the Collections.sort() method.

See the Java sorting tutorial and the API doc for each of the classes for more details.
[ September 28, 2006: Message edited by: Joanne Neal ]

Joanne
 
 
subject: Sort ArrayList
 
Similar Threads
sorting Arraylist of ArrayList
how we sort arraylist contents
please explain Generics compilation error
Sorting Names in SortNames
collections