• 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 in Java

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi *.*,
I have a general doubt in sorting of Java objects. I have a List which contain a set of java beans called "Cars". Each Cars bean has an attribute called "age". When I retrieve the List using another method, I dont get the List in a sorted manner.
I want to sort this List based on the attribute "age".
Which is the better apporach to do this sorting. Can I use Collection.sort() for this.
Here performance also is a problem. But one thing I can say is the size of the List is only about 20 max.

Thanks in Advance,
Regards,
Krishna Mohan R
rkmohan007@gmail.com
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use Collections.sort, but you'll need to use the one that takes a Comparator as the second argument (which you'll need to implement), because by itself the algorithm has no idea that the age attribute is the one that counts.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic