/** * Sort List by multiple conditions. * It's like SQL 'Order by' clause. Input multiple Comparator * in List as argument. * * @param * @param list * @param comparatorList * @throws IllegalArgumentException if comparatorList is empty * @author shengyuan.lu 卢声远 */ public static void sort(List list, final List > comparatorList) { if (comparatorList.isEmpty()) {//Always equals, if no Comparator. throw new IllegalArgumentException("comparatorList is empty."); } Comparator comparator = new Comparator () { public int compare(T o1, T o2) { for (Comparator c:comparatorList) { if (c.compare(o1, o2) > 0) { return 1; } else if (c.compare(o1, o2) < 0) { return -1; } } return 0; } }; Collections.sort(list, comparator); }
public void sortByMethod(List list, final String method, final boolean reverseFlag) { Collections.sort(list, new Comparator