- Collections.sort(mapList, new Comparator<Map<String, Object>>() {
- @Override
- public int compare(Map<String, Object> o1, Map<String, Object> o2) {
- if (o1.get("stopTime") == null && o2.get("stopTime") == null)
- return 0;
- if (o1.get("stopTime") == null)
- return -1;
- if (o2.get("stopTime") == null)
- return 1;
- return Long.valueOf(JSON.toJSONString(o2.get("stopTime"))).compareTo(Long.valueOf(JSON.toJSONString(o1.get("stopTime"))));
- }
- });
- //sort--下面是根据list的map中的一个时间字段排序的例子
- //List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
- Collections.sort(resultList, new Comparator<Map<String, Object>>() {
- public int compare(Map<String, Object> o1, Map<String, Object> o2) {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String s1 = o1.get("S_DATE").toString();
- String s2 = o2.get("S_DATE").toString();
- long val1 = 0;
- long val2 = 0;
- try {
- val1 = format.parse(s1).getTime();
- val2 = format.parse(s2).getTime();
- } catch (ParseException e) {
- }
- return (int) (val2 - val1);
- }
- });