Mar 27

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

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: , , , ,

Mar 27

Crazy about touchscreens? And you can’t afford to buy one?

If you’ve got a laptop and just crazy about touchscreens, then you have this wonderful gizmo called the Laptop Tablet from NAVIsis.

Touchscreen laptop

source : scifi

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags:

Give your best to the world.