views:

361

answers:

2

Hi, I have an collection of java beans that populate a JSF DataTable I'm trying to implement column sorting.

I'd like to sort the array/collection depending on the field selected. I have using Reflection in the past for this, but wanted to find a neater way of doing it, using Commons BeanUtils and/or Collections but can't seem to find any examples.

Thanks Scottyab

A: 

Actually after a bit of playing around here's what i come up with and it seems to work

String sortColumn = (String)getRequestParam("sort_id");

List quotes = (List)getSessionScope().get(SESS_SEARCH_RESULTS);

Comparator fieldCompare = new org.apache.commons.beanutils.BeanComparator( sortColumn ); Collections.sort(quotes, fieldCompare );

Just need to look at the sort order now :)

scottyab
A: 

not immediately relevant to your specific question, but look at GlazedLists -- it makes implementing this stuff for GUI's really easy.

Jason S