aspose file tools*
The moose likes Java in General and the fly likes How to sort an arraylist? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to sort an arraylist?" Watch "How to sort an arraylist?" New topic
Author

How to sort an arraylist?

sridevi kumar
Ranch Hand

Joined: Oct 17, 2001
Posts: 31
Hi:
I want to sort an array list (project requirement)? Could anyone guide me how to do this?
Thanks a lot...
Roger Thornhill
Author
Greenhorn

Joined: May 15, 2002
Posts: 25
Collections.sort() is a simple and easy way to do that. But remember that the object being sorted must implement Comparable.
sridevi kumar
Ranch Hand

Joined: Oct 17, 2001
Posts: 31
Thanks a lot Greg Barish.
Could u please tell me how to implement Comparable to sort the strings?
Thanks a lot again
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
The comparable interface declares one method called compareTo(). It returns -1,0, or 1 depending whether the current object is less than, equal to or greater than the one passed to the method. Check the API for more details.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Roger Thornhill
Author
Greenhorn

Joined: May 15, 2002
Posts: 25
BTW, Strings already implement Comparable, so you really don't need to do anything. Just stick the String objects in your ArrayList and call Collections.sort().
sridevi kumar
Ranch Hand

Joined: Oct 17, 2001
Posts: 31
Thank u very much Greg Barish and Bosun Bello.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by sridevi kumar:
Hi:
I want to sort an array list (project requirement)? Could anyone guide me how to do this?
Thanks a lot...

This should help
Jamie
 
 
subject: How to sort an arraylist?