Archive

Posts Tagged ‘compare two objects’

Compare two Objects using Comparator

March 27th, 2007 Arun Manivannan 5 comments

Old story but will be useful for sure.

Say you have an ArrayList of Beans (eg. ArrayList of Employee objects) that need to be sorted based on an attribute inside the Bean (eg. EmployeeName) before displaying to your GUI. You have two options before you..

1) If you are among the lucky lot who gets access to the Data Access Object (EJB or a class that has queries), just alter your query to return the ResultSet after sorting. i.e. use an “order by” clause in your query.

2) Or…. Use the Comparator interface. (you can use Comparable too. I am not talking about that in this post)

The implementation is pretty simple..

Here goes the code.

I have three classes with me

1) UserBean.java (which is just a bean Bean with regular getters and setters). It has three attributes inside it. userId, userName, address

2) UserNameComparator.java (which implements the Comparator interface and obviously overrides the compare method). The compare method just accepts two objects and returns an int. neednt worry about what to return as “int”. Just call the compareTo(Object) method inbuilt in String and Wrapper classes. It will return an int. Just use it.

3) SortUserBean.java (the caller method). Just builds a dummy ArrayList of UserBeans, displays the ArrayList before sorting and after sorting.

Download the complete source code