• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting Collection of objects by object members

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic