InApp   /   Our Leaders   /  

Object List Sorting Using BeanComparator

Object List Sorting Using BeanComparator

We can sort List<Objects> using BeanComparater instead of writing comparator. The beanutils.jar has to be imported. Default sort order is in ascending order.

For eg.
Collections.sort(postings, new BeanComparator(“resumeCount”));
OR
BeanComparator bc = new BeanComparator(“resumeCount”);
Collections.sort(postings, bc);
Collections.reverse(postings);
Pros:- Concise, Minimal code
Cons:- Low performance, uses reflection (now if a field is renamed, the compiler won’t even report a problem)