Sort Multiple Fields With Collections.sort

25 03 2008

ComparatorChain chain = new ComparatorChain();
Comparator comp = new BeanComparator("firstname", new NullComparator(true));
chain.addComparator(comp,true);
comp = new BeanComparator("lastname", new NullComparator(true));
chain.addComparator(comp);
Collections.sort(customerlist,chain);

NullComparator(true) <– true places null values at the front of the list
chain.addComparator(comp,true); <– true reverse the list
customerlist = list of dynabeans

Advertisements

Actions

Information

2 responses

2 03 2009
Jon

Ah this is handy, i could only write single comparators to sort collections. Didn’t know about ComparatorChain.

Thanks,

11 09 2012
SANSSAN

Thanks….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: