Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to sort an arraylist?

 
sridevi kumar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roger Thornhill
Author
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u very much Greg Barish and Bosun Bello.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic