${log.root}/lowem.log
Inflation, Investing and Everything


All | Energy | Java | Tech | Musings | Env | Biz

AddThis Feed Button
20051020 Thursday October 20, 2005

Dynamic bean sorting

Biow was asking for a way to dynamically sort an ArrayList of bean objects. The beans are your typical JavaBeans with private attributes and getter/setter methods for each attribute. The JDK Collections.sort() method provides for a custom Comparator that you can implement yourself, but it gets tedious very quickly to have to write a bunch of Comparators to sort each field and also having to take into account ascending/descending order.

I had something in mind about using reflection to dynamically invoke the getter method, and then getting the values, and putting that to work in a custom Comparator, but of course someone must have encountered the same problem before. York Davis has written an article on Dynamic Sorting With Java with source code for a DynamicComparator which does exactly what is needed. It accepts Collection, which is even better, which means you can throw in not only ArrayList, but also Vector and so on.

(2005-10-20 11:09:28 SGT) [Java] Permalink Comments [2]

Comments:

cool.. it's working.. *yay*..

p.s. u want to archive the article?

Posted by biow on October 20, 2005 at 12:08 PM SGT #

That's a good idea, using reflection.
To extrapolate from that, maybe the Comparator should expose a method, when given an object to be compared, look through all its getters and return a String[]. Nothing a PropertyUtils can't do. That will make it useful if you intend to allow the user to choose on the screen all the possible sorting combinations.
And since we're using PropertyUtils, how about sorting on indexed properties?

Posted by gwunwai on October 20, 2005 at 01:22 PM SGT #

Post a Comment:

Comments are closed for this entry.

Your Ad Here


Most popular blog postings on lowem.log :

1. Singapore MRT rail network length to double by 2020
2. 2010 Nissan Leaf electric car specifications : 107hp, 24KWh lithium-ion batteries, 100-mile range
3. Live spot gold price quotes chart on COMEX
4. 2010 Toyota Prius specifications released : 50 mpg, 1.8L, 134hp, Ni-MH, solar roof option
5. AVG Anti-Virus Free Edition 2011 direct download link
6. Real-time live gold and silver price quotes chart on COMEX
7. Singapore electric vehicles : Government agencies EMA and LTA to study EV introduction
8. Book review : Shut Down by William Flynn

Featured articles on lowem.log :

1. Book review : Shut Down by William Flynn
2. Singapore electric cars testing starts with 9 electric vehicles
3. Honda, GS Yuasa JV to make lithium-ion batteries for 2010/2011 Honda Civic Hybrid
4. 2010 Honda Civic Hybrid preliminary specifications released
5. 2010 Honda CR-Z hybrid, 2010 Honda Fit/Jazz hybrid models confirmed
6. 2010 Toyota Prius specifications released : 50 mpg, 1.8L, 134hp, Ni-MH, solar roof option
7. NYMEX crude oil recovers from $32.40 low after 2.2 mbpd OPEC production cut announced
8. Singapore : Nuclear power not ruled out



search
sponsored links


bookmarks

about
my profile
contact me

blogroll
biow/ken/wenn
reviewem
afternote

sites
photo gallery
wiki

quotes
live oil prices
live gold prices

charts
live forex rates
live oil chart
live brent crude chart
live gold chart
live silver chart

historical
crude oil chart
gold chart
silver chart


navigation
decals

Click for Singapore, Singapore Forecast





rss feed for lowem.log

Get Firefox!

powered by
hosted by