您的位置:首页 > 移动开发 > Objective-C

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

2017-06-09 16:52 573 查看
原文地址: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: