Java中的Comparator 对多个排序条件的处理

 

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());
		}
	}
}
 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值