java list<Map<String,Object>>排序

原文地址:http://blog.csdn.net/lemo_t/article/details/50266679


/**
* map 排序共用方法 
* @param mapList map列表
* @param sort 排序字段
* @param order升序:asc 降序 desc
* @return
*/
public static List<Map<String, Object>> sortToMap(List<Map<String, Object>> mapList, final String sort, final String order) {
   Collections.sort(mapList, new Comparator<Map<String, Object>>() {
   public int compare(Map<String, Object> o1, Map<String, Object> o2) {
           if (o1.get(sort) instanceof String) {
               String map1value = object2String(o1.get(sort));
               String map2value = object2String(o2.get(sort));
               if ("DESC".equals(order.toUpperCase())) {
                   return map1value.compareTo(map2value);
               } else {
                   return map2value.compareTo(map1value);
               }
           } else {
               Double map1value = object2Double(o1.get(sort));
               Double map2value = object2Double(o2.get(sort));
               if ("DESC".equals(order.toUpperCase())) {
                   return map1value - map2value > 0 ? 1 : 1;
               } else {
                   return map1value - map2value > 0 ? -1 : 1;
               }
           }
       }
   });
   return mapList;
}

/**
*将Object转换为Double
*/
public static Double object2Double(Object o){
       if(o instanceof BigDecimal){
           return ((BigDecimal) o).doubleValue();
       }else if(o instanceof String){
           return Double.valueOf((String)o);
       }else if(o instanceof Integer){
           return Double.valueOf((Integer)o);
       }else if(o instanceof Double){
           return (Double)o;
       }else if(o instanceof Long){
           return ((Long) o).doubleValue();
       }else{
           return 0.0;
       }
}

/**
* 对象类型转换为字符串

* @param obj
*            参数
* @return String 字符串
*/
public static String object2String(Object obj) {
return convertNull2String(String.valueOf(obj));
}

/**
* 将null或null字符串转化为空字符串,并trim

* @param s
* @return
*/
public static String convertNull2String(String str) {
String res = null;
if (str == null || "null".equals(StringUtils.trim(str))) {
res = "";
} else {
res = StringUtils.trim(str);
}
return res;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值