aspose file tools*
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
Author

Sorting Collection of objects by object members

venkatesh loganathan
Greenhorn

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
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
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

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