It's not a secret anymore!
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sort ArrayList" Watch "Sort ArrayList" New topic

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.


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

Joined: Aug 05, 2005
Posts: 3742
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 ]

I agree. Here's the link:
subject: Sort ArrayList
It's not a secret anymore!