你换在写n个比较器进行比较吗?
import java.util.Comparator;
import java.util.logging.Logger;
import org.apache.commons.beanutils.BeanUtils;
/**
* 排序的类的应用
* @author bailonggang
* 2008-12-7
* 上午11:21:51
*/
public class CompartorUtil implements Comparator<Object>{
/***对象的升序应用**/
public static final int SORT_DESC=0;
/***对象的降序应用**/
public static final int SORT_ASC=1;
private static Logger logger=Logger.getLogger(CompartorUtil.class.getName());
//排序的属性
private String property;
//排序的类型升降序
private int sortType;
public CompartorUtil(String property,int sortType)
{
this.property=property;
this.sortType=sortType;
}
/**
*排序的实现的类的
*/
@SuppressWarnings("unchecked")
public int compare(Object o1,Object o2) {
try {
String property0=BeanUtils.getProperty(o1, this.property);
String property1=BeanUtils.getProperty(o2, this.property);
int result=0;
result=property0.compareTo(property1);
if(SORT_DESC==this.sortType)
{
result=-result;
}
if(result>=1)
{
return 1;
}else if(result<=-1)
{
return -1;
}
return result;
} catch (Exception e) {
logger.info("对象排序时错误:"+e);
}
return 0;
}
}
如果有什么好的改进请告诉我谢谢,!!!