Sort java beans collector by field

I have a java beans set that populates a JTF DataTable. I am trying to sort columns.

I would like to sort an array / collection based on the selected field. I used Reflection in the past for this, but wanted to find a tidier way to do this using Commons BeanUtils and / or collections, but cannot find any examples.

Thanks Scottyab

+4
source share
2 answers

In fact, after a few games around, what I came 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 ); 

You just need to look at the sort order now.

+4
source

not directly related to your specific question, but look at GlazedLists - this makes it very easy to implement this material for the GUI.

0
source

Source: https://habr.com/ru/post/1285736/


All Articles