1. 程式人生 > >Util工具類 對map中含有String型別的日期key值的list進行排序

Util工具類 對map中含有String型別的日期key值的list進行排序

/**
 * 對含有map的list排序
 *
 * @param areaList 原始值
 * @param isDesc   TRUE:從大到小  FALSE:從小到大
 */
public static void sortListMap(List<Map.Entry<String, Double>> areaList, final boolean isDesc) {
    Collections.sort(areaList, new Comparator<Map.Entry<String, Double>>() {
        public int 
compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) { int flag = 1; if (isDesc) { if (o2.getValue() - o1.getValue() < 0) { flag = -1; } } else { if (o2.getValue() - o1.getValue() > 0
) { flag = -1; } } return flag; } }); } /** * 對map中含有String型別的日期key值的list進行排序 * <p> * 2017年9月29日 17:19:09 * xj * * @param list List<Map<String,Object>>,String為日期 * @param format 日期格式 * @param isDesc TRUE:從大到小 FALSE:從小到大 */ public static void
sortListStringDateMap(List list, final String format, final boolean isDesc) { Collections.sort(list, new Comparator() { @Override public int compare(Object o1, Object o2) { Map<String, Object> o1Map = (Map<String, Object>) o1; Map<String, Object> o2Map = (Map<String, Object>) o2; String o1Key = ""; for (String key : o1Map.keySet()) { o1Key = key; } String o2Key = ""; for (String key : o2Map.keySet()) { o2Key = key; } Integer o1K = Integer.valueOf(Util.transformDateToString(Util.transformStringToDate(o1Key, format), "yyyyMMdd")); Integer o2K = Integer.valueOf(Util.transformDateToString(Util.transformStringToDate(o2Key, format), "yyyyMMdd")); int flag = 1; if (isDesc) { if (o2K - o1K < 0) { flag = -1; } } else { if (o2K - o1K > 0) { flag = -1; } } return flag; } }); }