Sorting an collecton of java beans by field
-
09-09-2019 - |
Question
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
Solution
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<Quote> quotes = (List<Quote>)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 :)
OTHER TIPS
not immediately relevant to your specific question, but look at GlazedLists -- it makes implementing this stuff for GUI's really easy.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow