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


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

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.




Most popular blog postings on lowem.log :

1. Singapore SIBOR interest rates fall to 1.5%, lowest since Dec 2004
2. 2009 Honda Global Small Hybrid details released : bigger than Jazz/Fit, smaller than Civic
3. Singapore : Inflation rate could push past 6% in Q1 2008
4. Singapore SIBOR rate fell to 1.25% in Apr 2008, lowest since Aug 2004
5. Fuel prices seen stoking Malaysia inflation in 2008
6. Singapore CPI inflation rate for May 2008 continues at 26-year high of 7.5%
7. Singapore CPI inflation hits 6.6% in Jan 2008 - a new 25-year record high
8. Singapore SIBOR rate falls to 1.31%, lowest since Nov 2004

Featured articles on lowem.log :

1. ABC Guide to Beating Inflation in Singapore and Elsewhere
2. Singapore inflation rate hits new 26-year high of 7.5% in Apr 2008
3. Hyper-inflation : early warning signs
4. Singapore hyperinflation warning signs #2 : Cooking oil price up 75%
5. Bread and inflation
6. Singapore M3 money supply growth vs STI stock index
7. Singapore SIBOR rate fell to 1.25% in Apr 2008, lowest since Aug 2004
8. Singapore : Inflation erodes away bank savings





| << ^ | 5< | << | + | Peak Oil Webring | ? | >> | >5 | ^ >> |
archives
search
sponsored links





bookmarks

personal
biow
ken
wenn

sites
photo gallery
wiki

blogroll
reviewem
sgenergycrisis
theenergycollective

forums
goldclubasia.com
peakoil.com


navigation
decals

Click for Singapore, Singapore Forecast



add rss feed to my yahoo!

rss feed for lowem.log

foaf.rdf description for lowem

Get Firefox!

powered by
hosted by