直接上代码,一看既懂:
/**
* 根据某一属性排序
* @param objs 数据
* @param i 根据第几个字段排序
* @return
*/
public List<Object> orderList(List<Object> objs,int i,String order){
Collections.sort(objs,new Comparator<Object>(){
public int compare(Object arg0, Object arg1){
Object[] obj0Arr = (Object[])arg0;
BigDecimal a = obj0Arr[i]==null?new BigDecimal(0):new BigDecimal(obj0Arr[i].toString());
Object[] obj1Arr = (Object[])arg1;
BigDecimal b = obj1Arr[i]==null?new BigDecimal(0):new BigDecimal(obj1Arr[i].toString());
if("asc".equals(order)){
return b.compareTo(a);//由小到大
}else{
return a.compareTo(b);//由大到小
}
}
});
return objs;
}