对List< Map >数据排序
原始数据
{"msg":"成功","code":0,"obj":[{"importance":3,"childList":[{"importance":3,"kpId":2091,"kpName":"增值税应纳税额的计算-一般计税方法应纳税额的计算","attrStatus":2,"kpScore":1.00,"isExamPoint":1,"baseKpId":2139,"isMust":1},{"importance":3,"kpId":2093,"kpName":"增值税应纳税额的计算-简易计税方法应纳税额的计算","attrStatus":2,"kpScore":1.00,"isExamPoint":1,"baseKpId":2141,"isMust":1},{"importance":3,"kpId":2095,"kpName":"增值税应纳税额的计算-进口货物应纳税额的计算","attrStatus":2,"kpScore":0.67,"isExamPoint":1,"baseKpId":2143,"isMust":1}],"kpName":"增值税应纳税额的计算","kpId":2089,"attrStatus":2,"kpScore":0.00,"isExamPoint":1,"baseKpId":2137,"isMust":1},{"importance":3,"childList":[{"importance":3,"kpId":2063,"kpName":"增值税征税范围-销售货物","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2111,"isMust":1},{"importance":3,"kpId":2065,"kpName":"增值税征税范围-销售劳务","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2113,"isMust":1},{"importance":3,"kpId":2067,"kpName":"增值税征税范围-销售服务","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2115,"isMust":1},{"importance":3,"kpId":2069,"kpName":"增值税征税范围-销售无形资产","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2117,"isMust":1},{"importance":3,"kpId":2071,"kpName":"增值税征税范围-销售不动产","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2119,"isMust":1},{"importance":3,"kpId":2073,"kpName":"增值税征税范围-进口货物","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2121,"isMust":1},{"importance":3,"kpId":2075,"kpName":"增值税征税范围-非经营活动的界定","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2123,"isMust":1},{"importance":3,"kpId":2077,"kpName":"增值税征税范围-境内销售服务、无形资产或者不动产的界定","attrStatus":2,"kpScore":0.20,"isExamPoint":1,"baseKpId":2125,"isMust":1},{"importance":3,"kpId":2079,"kpName":"增值税征税范围-视同销售货物行为","attrStatus":2,"kpScore":0.19,"isExamPoint":1,"baseKpId":2127,"isMust":1},{"importance":3,"kpId":2081,"kpName":"增值税征税范围-混合销售","attrStatus":2,"kpScore":0.00,"isExamPoint":1,"baseKpId":2129,"isMust":1},{"importance":3,"kpId":2083,"kpName":"增值税征税范围-兼营","attrStatus":2,"kpScore":0.00,"isExamPoint":1,"baseKpId":2131,"isMust":1},{"importance":3,"kpId":2085,"kpName":"增值税征税范围-不征收增值税项目","attrStatus":2,"kpScore":0.19,"isExamPoint":1,"baseKpId":2133,"isMust":1}],"kpName":"增值税征税范围","kpId":2061,"attrStatus":2,"kpScore":0.00,"isExamPoint":1,"baseKpId":2109,"isMust":1},{"importance":3,"childList":[{"importance":3,"kpId":2113,"kpName":"消费税应纳税额的计算-销售额的确定","attrStatus":2,"kpScore":0.50,"isExamPoint":1,"baseKpId":2161,"isMust":1},{"importance":3,"kpId":2115,"kpName":"消费税应纳税额的计算-应纳税额的计算","attrStatus":2,"kpScore":0.50,"isExamPoint":1,"baseKpId":2163,"isMust":1},{"importance":3,"kpId":2117,"kpName":"消费税应纳税额的计算-已纳消费税的扣除","attrStatus":2,"kpScore":0.49,"isExamPoint":1,"baseKpId":2165,"isMust":1}],"kpName":"消费税应纳税额的计算","kpId":2111,"attrStatus":2,"kpScore":0.00,"isExamPoint":1,"baseKpId":2159,"isMust":1},{"importance":1,"childList":[],"kpName":"税收与税收法律关系","kpId":2053,"attrStatus":2,"isExamPoint":1,"baseKpId":2101,"isMust":0},{"importance":1,"childList":[],"kpName":"税法要素","kpId":2055,"attrStatus":2,"isExamPoint":1,"baseKpId":2103,"isMust":1},{"importance":1,"childList":[],"kpName":"现行税种与征收机关","kpId":2057,"attrStatus":2,"isExamPoint":1,"baseKpId":2105,"isMust":1},{"importance":2,"childList":[],"kpName":"增值税纳税人和扣缴义务人","kpId":2059,"attrStatus":2,"isExamPoint":1,"baseKpId":2107,"isMust":1},{"importance":1,"childList":[],"kpName":"增值税税率和征收率","kpId":2087,"attrStatus":2,"isExamPoint":1,"baseKpId":2135,"isMust":1},{"importance":2,"childList":[],"kpName":"增值税税收优惠","kpId":2097,"attrStatus":2,"isExamPoint":1,"baseKpId":2145,"isMust":1},{"importance":2,"childList":[],"kpName":"增值税征收管理","kpId":2099,"attrStatus":2,"isExamPoint":1,"baseKpId":2147,"isMust":1},{"importance":2,"childList":[],"kpName":"增值税专用发票使用规定","kpId":2101,"attrStatus":2,"isExamPoint":1,"baseKpId":2149,"isMust":1},{"importance":2,"childList":[],"kpName":"消费税纳税人","kpId":2103,"attrStatus":2,"isExamPoint":1,"baseKpId":2151,"isMust":1},{"importance":3,"childList":[],"kpName":"消费税征税范围","kpId":2105,"attrStatus":2,"isExamPoint":1,"baseKpId":2153,"isMust":1},{"importance":2,"childList":[],"kpName":"消费税税目","kpId":2107,"attrStatus":2,"isExamPoint":1,"baseKpId":2155,"isMust":1},{"importance":1,"childList":[],"kpName":"消费税税率","kpId":2109,"attrStatus":2,"isExamPoint":1,"baseKpId":2157,"isMust":1},{"importance":2,"childList":[],"kpName":"消费税征收管理","kpId":2119,"attrStatus":2,"isExamPoint":1,"baseKpId":2167,"isMust":1},{"importance":1,"childList":[],"kpName":"第四章小结","kpId":2121,"attrStatus":2,"isExamPoint":0,"baseKpId":2169,"isMust":0}]}
排序规则
按kpScore倒序排列
list.stream().sorted(Comparator.comparing(MapCompare::comparingByKpScore).reversed()) .collect(Collectors.toList());
MapCompare类
public class MapCompare { public static BigDecimal comparingByKpScore(Map map){ return (BigDecimal) map.get("kpScore"); } //、、、、、、如果需要其他字段排序可在下方补充 }
具体代码
public List<Map> getKpListByChapterIdOrder(String chapterId) { //获取原始数据 List<Map> resuList = getKpListByChapterId(chapterId.toString()); //对原始数据字段进行整理 //返回值list List<Map> chapterKpIds = new ArrayList<>(); Map resuMap; //遍历取子级知识点 for (Map map : resuList) { //判断是否有子级 List<Map> childList = (List<Map>) map.get("childList"); if (CollectionUtils.isNotEmpty(childList)) { //有子级知识点,则取子级知识点 for (Map cm : childList) { resuMap = new HashMap(8); resuMap.put("kpId",cm.get("kpId").toString()); resuMap.put("kpName",cm.get("kpName")); resuMap.put("kpScore",cm.get("kpScore")); chapterKpIds.add(resuMap); } }else { //没有子级则取父级 resuMap = new HashMap(8); resuMap.put("kpId",map.get("kpId").toString()); resuMap.put("kpName",map.get("kpName")); resuMap.put("kpScore",map.get("kpScore")); chapterKpIds.add(resuMap); } } //按kpScore倒序排列 chapterKpIds = chapterKpIds.stream().sorted(Comparator.comparing(MapCompare::comparingByKpScore).reversed()) .collect(Collectors.toList()); //返回数据 return chapterKpIds; }
多重条件排序时
list.stream().sorted(Comparator.comparing(MapCompare::comparingByKpScore).reversed() .thenComparing(Comparator.comparing(MapCompare::comparingByOther))) .collect(Collectors.toList());
排序后结果
[{"kpScore":1.20,"kpName":"消费税征税范围","kpId":"2105"},{"kpScore":1.15,"kpName":"增值税专用发票使用规定","kpId":"2101"},{"kpScore":1.05,"kpName":"增值税税收优惠","kpId":"2097"},{"kpScore":1.00,"kpName":"增值税应纳税额的计算-一般计税方法应纳税额的计算","kpId":"2091"},{"kpScore":1.00,"kpName":"增值税应纳税额的计算-简易计税方法应纳税额的计算","kpId":"2093"},{"kpScore":0.67,"kpName":"增值税应纳税额的计算-进口货物应纳税额的计算","kpId":"2095"},{"kpScore":0.67,"kpName":"消费税税率","kpId":"2109"},{"kpScore":0.62,"kpName":"现行税种与征收机关","kpId":"2057"},{"kpScore":0.62,"kpName":"增值税征收管理","kpId":"2099"},{"kpScore":0.52,"kpName":"消费税税目","kpId":"2107"},{"kpScore":0.50,"kpName":"消费税应纳税额的计算-销售额的确定","kpId":"2113"},{"kpScore":0.50,"kpName":"消费税应纳税额的计算-应纳税额的计算","kpId":"2115"},{"kpScore":0.49,"kpName":"消费税应纳税额的计算-已纳消费税的扣除","kpId":"2117"},{"kpScore":0.43,"kpName":"增值税纳税人和扣缴义务人","kpId":"2059"},{"kpScore":0.43,"kpName":"消费税征收管理","kpId":"2119"},{"kpScore":0.20,"kpName":"增值税征税范围-销售货物","kpId":"2063"},{"kpScore":0.20,"kpName":"增值税征税范围-销售劳务","kpId":"2065"},{"kpScore":0.20,"kpName":"增值税征税范围-销售服务","kpId":"2067"},{"kpScore":0.20,"kpName":"增值税征税范围-销售无形资产","kpId":"2069"},{"kpScore":0.20,"kpName":"增值税征税范围-销售不动产","kpId":"2071"},{"kpScore":0.20,"kpName":"增值税征税范围-进口货物","kpId":"2073"},{"kpScore":0.20,"kpName":"增值税征税范围-非经营活动的界定","kpId":"2075"},{"kpScore":0.20,"kpName":"增值税征税范围-境内销售服务、无形资产或者不动产的界定","kpId":"2077"},{"kpScore":0.19,"kpName":"增值税征税范围-视同销售货物行为","kpId":"2079"},{"kpScore":0.19,"kpName":"增值税征税范围-不征收增值税项目","kpId":"2085"},{"kpScore":0.19,"kpName":"税收与税收法律关系","kpId":"2053"},{"kpScore":0.19,"kpName":"增值税税率和征收率","kpId":"2087"},{"kpScore":0.00,"kpName":"增值税征税范围-混合销售","kpId":"2081"},{"kpScore":0.00,"kpName":"增值税征税范围-兼营","kpId":"2083"},{"kpScore":0.00,"kpName":"税法要素","kpId":"2055"},{"kpScore":0.00,"kpName":"消费税纳税人","kpId":"2103"},{"kpScore":0.00,"kpName":"第四章小结","kpId":"2121"}]