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 performace, Uses reflection(Now if a field is renamed, the compiler won’t even report a problem)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>