class GroupComparator<T> implements Comparator<T> {
/**排序方式**/
public static final String DESC = "DESC";
public static final String ASC = "ASC";
/**数组下标0**/
public static final int SORT_NAME = 0;
/**数组下标1**/
public static final int SORT_TYPE = 1;
/**存储比较器**/
private List<Comparator<T>> comparators = new ArrayList<Comparator<T>>();
public GroupComparator(final String[] ... sortCriteria) {
for (final String[] criteria : sortCriteria) {
Comparator<T> sortComparator = new Comparator<T> () {
public int compare(T t1, T t2) {
int result = 0;
try {
String name = criteria[GroupComparator.SORT_NAME];
String type = criteria[GroupComparator.SORT_TYPE];
Field field = t1.getClass().getDeclaredField(name);
Type fieldType = field.getType();
String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
Method method = t1.getClass().getDeclaredMethod(methodName, null);
Object o1 = method.invoke(t1, null);
Object o2 = method.invoke(t2, null);
if (fieldType == Integer.TYPE || fieldType == Long.TYPE || fieldType == Double.TYPE || fieldType == Float.TYPE || fieldType == Short.TYPE || fieldType == Byte.TYPE) {
result = type.equals(GroupComparator.DESC) ? (Integer) o2 - (Integer) o1 : (Integer) o1 - (Integer) o2;
} else if (fieldType == String.class || fieldType == Character.class) {
String str1 = o1 == null ? "" : o1.toString();
String str2 = o2 == null ? "" : o2.toString();
result = type.equals(GroupComparator.DESC) ? str2.compareTo(str1) : str1.compareTo(str2);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
};
comparators.add(sortComparator);
}
}
public int compare(T t1, T t2) {
for (Comparator<T> comparator : comparators) {
int returnValue = comparator.compare(t1, t2);
if (returnValue != 0) return returnValue;
}
return 0;
}
}
class Employee {
public static final String SORT_CRITERIA_NAME = "name";
public static final String SORT_CRITERIA_LEVEL = "level";
public static final String SORT_CRITERIA_SALARY = "salary";
public static final String SORT_CRITERIA_YEAR = "year";
private String name;
private int id;
private int level;
private int salary;
private int year;
public Employee(int id, String name, int level, int salary, int year) {
this.id = id;
this.name = name;
this.level = level;
this.salary = salary;
this.year = year;
}
//省略get、set方法...
}
public class MultipleComparatorTest {
public static void main(String[] args) {
List<Employee> objs = new ArrayList<Employee>() {
{
add(new Employee(5, "aa", 3, 5000, 2));
add(new Employee(1, "ac", 9, 10000, 10));
add(new Employee(4, "bb", 5, 8000, 6));
add(new Employee(2, "rrrrr", 9, 12000, 7));
add(new Employee(6, "kkkk", 1, 2000, 1));
add(new Employee(3, "ccc", 5, 8000, 12));
}
};
String[][] sortCriteria = {
{ Employee.SORT_CRITERIA_NAME, GroupComparator.ASC},
{ Employee.SORT_CRITERIA_LEVEL, GroupComparator.DESC },
{ Employee.SORT_CRITERIA_SALARY, GroupComparator.ASC },
{ Employee.SORT_CRITERIA_YEAR, GroupComparator.DESC }
};
GroupComparator<Employee> gc = new GroupComparator<Employee>(sortCriteria);
Collections.sort(objs, gc);
for (Employee em : objs) {
System.out.println(em.getName() + "-" + em.getLevel() + "-" + em.getSalary() + "-" + em.getYear());
}
}
}