This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Sorting Arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting Arraylist" Watch "Sorting Arraylist" New topic
Author

Sorting Arraylist

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
In a ArrayList , I have 10 records with columns Name , Age , Salary.
I want to sort ArrayList based on Age. How to do this
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
If the items contained in the ArrayList implement Comparable and if the compareTo method uses the age as the basis for comparison, you're set, simply call Collections.sort(yourArrayList) on your array list. If not, then you'll want to create a Comparator class that has a compare method that uses age as a basis for comparison and use an object of that class in the Collections.sort(yourArrayList, yourComparatorObject) method overload that takes a Comparator. Have a look at the Collections class and you'll see what I mean.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
This Java™ Tutorials section might help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting Arraylist
 
Similar Threads
Age Limit to Taking the SCJA Exam
Code Review - ChatServlet
Putting Tokens into an Array
How to store a name in an arraylist?
Sort Arraylist by passing runtime parameter