码迷,mamicode.com
首页 > 编程语言 > 详细

对List<Map<String,Obejct>> 类型的数据进行时间排序(降序)- 20180408

时间:2018-04-08 18:18:39      阅读:1489      评论:0      收藏:0      [点我收藏+]

标签:catch   compare   str   sim   ToJson   int   simple   com   except   

  1. Collections.sort(mapList, new Comparator<Map<String, Object>>() {  
  2.             @Override  
  3.             public int compare(Map<String, Object> o1, Map<String, Object> o2) {  
  4.                 if (o1.get("stopTime") == null && o2.get("stopTime") == null)  
  5.                     return 0;  
  6.                 if (o1.get("stopTime") == null)  
  7.                     return -1;  
  8.                 if (o2.get("stopTime") == null)  
  9.                     return 1;  
  10.                 return Long.valueOf(JSON.toJSONString(o2.get("stopTime"))).compareTo(Long.valueOf(JSON.toJSONString(o1.get("stopTime"))));  
  11.             }  
  12.         });  

 

 

  1.       //sort--下面是根据list的map中的一个时间字段排序的例子  
  2.         //List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();  
  3.         Collections.sort(resultList, new Comparator<Map<String, Object>>() {  
  4.             
  5.             public int compare(Map<String, Object> o1, Map<String, Object> o2) {  
  6.                 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  7.                 String s1 = o1.get("S_DATE").toString();  
  8.                 String s2 = o2.get("S_DATE").toString();  
  9.                 long val1 = 0;  
  10.                 long val2 = 0;  
  11.                 try {  
  12.                     val1 = format.parse(s1).getTime();  
  13.                     val2 = format.parse(s2).getTime();  
  14.                 } catch (ParseException e) {  
  15.                       
  16.                 }  
  17.                 return (int) (val2 - val1);  
  18.             }  
  19.         });  

对List<Map<String,Obejct>> 类型的数据进行时间排序(降序)- 20180408

标签:catch   compare   str   sim   ToJson   int   simple   com   except   

原文地址:https://www.cnblogs.com/naliyixin/p/8745725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!