public class SortTest { public static void main(String[] args) { //普通对象list sortVOList(); //mapList sortMapList(); } private static void sortVOList() { List<RiskMonitorScrollVO> voList = new ArrayList<>(); RiskMonitorScrollVO vo = new RiskMonitorScrollVO(); vo.setTime("202303"); vo.setOrgName("机构001"); vo.setOrgSortNo("001"); vo.setTargetName("指标001"); vo.setTargetSortNo("001"); voList.add(vo); vo = new RiskMonitorScrollVO(); vo.setTime("202303"); vo.setOrgName("机构003"); vo.setOrgSortNo("004"); vo.setTargetName("指标001"); vo.setTargetSortNo("001"); voList.add(vo); vo = new RiskMonitorScrollVO(); vo.setTime("202303"); vo.setOrgName("机构002"); vo.setOrgSortNo("003"); vo.setTargetName("指标001"); vo.setTargetSortNo("001"); voList.add(vo); vo = new RiskMonitorScrollVO(); vo.setTime("202303"); vo.setOrgName("机构004"); vo.setOrgSortNo("002"); vo.setTargetName("指标002"); vo.setTargetSortNo("002"); voList.add(vo); vo = new RiskMonitorScrollVO(); vo.setTime("202202"); vo.setOrgName("机构001"); vo.setOrgSortNo("001"); vo.setTargetName("指标001"); vo.setTargetSortNo("001"); voList.add(vo); vo = new RiskMonitorScrollVO(); vo.setTime("202210"); vo.setOrgName("机构001"); vo.setOrgSortNo("001"); vo.setTargetName("指标001"); vo.setTargetSortNo("001"); voList.add(vo); System.out.println(JSON.toJSONString(voList)); voList.sort(Comparator.comparing(RiskMonitorScrollVO::getTime, Comparator.reverseOrder()) .thenComparing(RiskMonitorScrollVO::getOrgSortNo) .thenComparing(RiskMonitorScrollVO::getTargetSortNo)); System.out.println(JSON.toJSONString(voList)); } private static void sortMapList() { List<Map<String, Object>> list = new ArrayList<>(); Map<String, Object> map = new HashMap<>(); map.put("orgId", 1); map.put("time", "202301"); list.add(map); map = new HashMap<>(); map.put("orgId", 1); map.put("time", "202212"); list.add(map); map = new HashMap<>(); map.put("orgId", 3); map.put("time", "202202"); list.add(map); map = new HashMap<>(); map.put("orgId", 2); map.put("time", "202301"); list.add(map); System.out.println(list); list.sort(Comparator.comparing(x -> ((Integer) ((Map<String, Object>) x).get("orgId"))) .thenComparing(x -> ((String) ((Map<String, Object>) x).get("time"))) ); System.out.println(list); } }
标签:map,java,List,add,vo,MapList,001,voList,new From: https://www.cnblogs.com/hdwang/p/17240320.html