package com.liuyc.drools.test;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@SuppressWarnings("unchecked")
public class ComparatorUtil implements Comparator<Object> {
private String method;
private Class<Object> objClass;
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public Class<Object> getObjClass() {
return objClass;
}
public void setObjClass(Class<Object> objClass) {
this.objClass = objClass;
}
public ComparatorUtil(Class objClass, String method) {
this.setMethod(method);
this.setObjClass(objClass);
}
/**
* 实现list中bean的某个属性(Int/Integer类型)的升降排序.
* @param list 需要排序的类
* @param objClass bean的class
* @param getMethod 需要排序属性的get方法
* @param order true升序,false降序.
*/
public static void compareList(List list,Class objClass,String getMethod,boolean order){
ComparatorUtil util = new ComparatorUtil(objClass, getMethod);
if (order) {
Collections.sort(list, util);
}else{
Collections.sort(list, util);
//这步骤觉得有点怪..
Collections.reverse(list);
}
}
public int compare(Object obj1, Object obj2) {
int value1 = this.getObjValue(obj1);
int value2 = this.getObjValue(obj2);
if ( value1 < value2 ) {
return -1;
} else if (value1 == value2) {
return 0;
}else{
return 1;
}
}
/**
* 获得对象属性的值.
* @param obj
* @return .
*/
private int getObjValue(Object obj) {
Method getMethod = null;
int getValue = 0;
try {
getMethod = obj.getClass().getMethod(this.getMethod(), new Class[] {});
Object value = getMethod.invoke(obj, new Object[] {});
if (value != null) {
if (value instanceof Integer) {
getValue = Integer.parseInt(value.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return getValue;
}
}
发了点时间写了下.感觉还是有点不对..请求高手改正.....
参考路径:http://topic.csdn.net/u/20090523/17/05771830-8b54-4d91-ab6b-ddde8557a9fc.html 我只是将对象改成反射来写..顺便加了点小把戏..
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@SuppressWarnings("unchecked")
public class ComparatorUtil implements Comparator<Object> {
private String method;
private Class<Object> objClass;
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public Class<Object> getObjClass() {
return objClass;
}
public void setObjClass(Class<Object> objClass) {
this.objClass = objClass;
}
public ComparatorUtil(Class objClass, String method) {
this.setMethod(method);
this.setObjClass(objClass);
}
/**
* 实现list中bean的某个属性(Int/Integer类型)的升降排序.
* @param list 需要排序的类
* @param objClass bean的class
* @param getMethod 需要排序属性的get方法
* @param order true升序,false降序.
*/
public static void compareList(List list,Class objClass,String getMethod,boolean order){
ComparatorUtil util = new ComparatorUtil(objClass, getMethod);
if (order) {
Collections.sort(list, util);
}else{
Collections.sort(list, util);
//这步骤觉得有点怪..
Collections.reverse(list);
}
}
public int compare(Object obj1, Object obj2) {
int value1 = this.getObjValue(obj1);
int value2 = this.getObjValue(obj2);
if ( value1 < value2 ) {
return -1;
} else if (value1 == value2) {
return 0;
}else{
return 1;
}
}
/**
* 获得对象属性的值.
* @param obj
* @return .
*/
private int getObjValue(Object obj) {
Method getMethod = null;
int getValue = 0;
try {
getMethod = obj.getClass().getMethod(this.getMethod(), new Class[] {});
Object value = getMethod.invoke(obj, new Object[] {});
if (value != null) {
if (value instanceof Integer) {
getValue = Integer.parseInt(value.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return getValue;
}
}
发了点时间写了下.感觉还是有点不对..请求高手改正.....
参考路径:http://topic.csdn.net/u/20090523/17/05771830-8b54-4d91-ab6b-ddde8557a9fc.html 我只是将对象改成反射来写..顺便加了点小把戏..