Java Tips Weblog

  • Blog Stats

    • 1,845,415 hits
  • Categories

  • Archives

Archive for the ‘Util’ Category

Bean Comparator

Posted by Rob Camick on October 23, 2008

Java provides basic support for sorting Beans stored in Lists and Arrays by using the Collections.sort(…) and Arrays.sort(…) methods respectively. If you want your Bean to be sortable, then your Bean must implement the Comparable interface. This will provide your Bean with a “Natural Order” sort. What do you do when you want to sort your Bean on a different property? Well, in this case you will need to create a custom Comparator to be used by the sort methods. But, do we really need to create custom Comparators for each sortable property, or is there an easier way?
Read the rest of this entry »

Posted in Classes, Util | 9 Comments »

Group Comparator

Posted by Rob Camick on October 17, 2008

Many times a sort may need to be performed on multiple fields to get the desired result. For example, in a phone book “First Name” is sorted within “Last Name”. Using the existing Java sort utilities, Collections.sort(…) and Arrays.sort(…), this would be done by first doing a sort on the First Name and then a sort on the Last Name. Obviously it is not very efficient to perform a double sort. A better approach would be to sort on both fields in a single pass.
Read the rest of this entry »

Posted in Classes, Util | 2 Comments »

Column Comparator

Posted by Rob Camick on October 16, 2008

Java provides basic support for sorting data in Lists and Arrays by using the Collections.sort(…) and Arrays.sort(…) methods respectively. This works great when you have a single column of data in the List, an Array of Strings or a List of Integers, for example. But what happens when the data to be sorted is a List or an Array itself? In this case you would want to sort on a specific column and the data type to be sorted may be unknown. Java does not support this so what can you do?
Read the rest of this entry »

Posted in Classes, Util | 7 Comments »