Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Collection of objects by object members

 
venkatesh loganathan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I have to sort a collection of objects based onthe object property.If the object has firstname as a member then sortg should be based on that.Any hep regarding this.

Venkatesh Loganthan.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually your question does not belong to this forum, but Java in General Forum...

but I'm giving you some suggestion on your problem... To deal with the properties in objects, it'd be better to put the property that you have to sort in another collection and provide some kind of pointing technique to point to the real objects in another collections...
For example, one collection contains a list of firstnames associated with a key to its own object in another collection... This approach might help you in dealing with sorting other properties as well...

Hope it helps...
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

U can use Arrays.sort(list,comparator) u can use this and u shouls override the compareTo method in the comparator object and this will help insorting the objects depending on the criteria u need
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@see the JDK JavaDoc for Collections.sort and Comparator.

Write a comparator. Pass your collection and your comparator to Collections.sort. Implementation done.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic