This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting Arraylist  RSS feed

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This Java™ Tutorials section might help
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!