wood burning stoves 2.0
The moose likes Struts and the fly likes Sorting Collection of objects by  object members Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Sorting Collection of objects by  object members" Watch "Sorting Collection of objects by  object members" New topic

Sorting Collection of objects by object members

venkatesh loganathan

Joined: Aug 24, 2004
Posts: 14
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

Joined: Jun 08, 2002
Posts: 3178
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...

Co-author of SCMAD Exam Guide, Author of JMADPlus
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415

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

Joined: Dec 19, 2002
Posts: 374
@see the JDK JavaDoc for Collections.sort and Comparator.

Write a comparator. Pass your collection and your comparator to Collections.sort. Implementation done.

"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
I agree. Here's the link: http://aspose.com/file-tools
subject: Sorting Collection of objects by object members
It's not a secret anymore!