将Map<String, List<Map<String,Object>>>进行排序

首先我贴上我的代码,刚开始我也不知道怎么排序还写了一些方法,最后请教群里的大神解决了

public Map<String, List<Map<String,Object>>> getGrowList(String id){
        
        List<Map<String, Object>> growList = mentDao.getGrowList(id);
        SortedMap<String, List<Map<String,Object>>> maps = new TreeMap<>();
        List<Map<String,Object>> list = null;
        Map<String,Object> grothMap = null;
        for (Map<String, Object> map2 : growList) {
            String dt = map2.get("createTime").toString();
            String month = dt.substring(0, 10);
            String time = dt.substring(10,16);
            list = maps.get(month);
            if(null == list){
                list = new ArrayList<Map<String,Object>>();
                maps.put(month, list);
            }
            grothMap = new HashMap<String,Object>();
            grothMap.put("time", time);
            grothMap.put("note", map2.get("notes").toString());
            list.add(grothMap);
        }
        return maps;
        
    }

我的数据格式是这样的

{
2015-06-29=[
{
time=00: 00,
note=小羊出生
}
],
2015-10-21=[
{
time=10: 07,
note=入栏养殖
}
],
2015-09-23=[
{
time=03: 27,
note=开始建档
}
]
}

我是想根据年月日来排序,这里已经自动排序了.首先我申明了SortedMap<String, List<Map<String,Object>>> maps = new TreeMap<>();这个变量因为SortedMap是继承Map的所以直接可以用这个方法

现在我来说说他根据年月日来排序了也将我的time排序了,刚开始我也奇怪,因为time是在我的年月日的集合里,为什么也给我排序了,后来仔细看了代码想想,根据个人的理解他是首先根据growList来排序,里面的时间来排序的,因为我的growList的格式是这样的

[
{
notes=小羊出生,
createTime=2015-06-2900: 01: 00.0
},
{
notes=体重,
createTime=2015-06-2909: 27: 01.0
},
{
notes=测量体重,
createTime=2015-06-2910: 08: 01.0
},
{
notes=开始建档,
createTime=2015-09-2303: 27: 53.0
},
{
notes=入栏养殖,
createTime=2015-10-2110: 07: 22.0
}
]

我每截取一次createTime放到list,就给我排序了一次 因为list = maps.get(month); maps将截取的时间排序了放到list里面了 ,所以我的time也给我自动排序了.如果有不同意见的请提出,我会加紧修正

原文地址:https://www.cnblogs.com/HYXJavaweb/p/5062713.html