Posts

Showing posts from January, 2011

How to sort Java collection on multiple columns

Image
Java collection API, provide Comparator interface to sort the Java collection such as List. However, multi column sort is not possible. Multi column sort can be achieved using two Comparators in two different Collections.sort(Comparator...) calls. I am wonder why sort() method doesn't allow for varargs which is new in Java 5. Due to the limitation of sorting one column at once may giving performance problem (I don't know). Apache common has facility do multi column sorting. package com.oj.ex; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; import org.apache.commons.collections.comparators.ComparatorChain; public class TestSorting { /** * @param args */ @SuppressWarnings("unchecked") public static void main(String[] args) { List<Payment> payments = new ArrayList<Pa...

IBM developerWorks : Java Technology : Technical library

IBM developerWorks : Java Technology : Technical library : These are very good articles can be found in the IBM developer works.