java实现并列占位排名,支持并列排序

一、功能说明:

1.实现并列占位排名,例如两个第一名,排序为:1、1、3

2.实现并列数据再次排名,例如两个第一名,学号在前的拍前面

二、缺点:

不适合大规模数据,其他问题暂未发现,欢迎补充

三、实现步骤和逻辑

1.根据分数将数据分组成map

2.将分组后的map转成list并按照分数排序

3.遍历list,将list中map中的list按照序号进行排序,实现并列数据排序

4.拼接最后的list

四、代码

//创建假数据List> list = new ArrayList<>();Map map1 = new HashMap<>();map1.put("name", "罗1彤");  //名字map1.put("sort", "6");      //排序map1.put("fraction", "99"); //分数list.add(map1);Map map2 = new HashMap<>();map2.put("name", "罗2彤");map2.put("sort", "5");map2.put("fraction", "99");list.add(map2);Map map3 = new HashMap<>();map3.put("name", "罗3彤");map3.put("sort", "4");map3.put("fraction", "98");list.add(map3);Map map4 = new HashMap<>();map4.put("name", "罗4彤");map4.put("sort", "3");map4.put("fraction", "98");list.add(map4);Map map5 = new HashMap<>();map5.put("name", "罗5彤");map5.put("sort", "2");map5.put("fraction", "97");list.add(map5);Map map6 = new HashMap<>();map6.put("name", "罗6彤");map6.put("sort", "1");map6.put("fraction", "100");list.add(map6);//数据根据 分数 进行分组 后排序List>>> tempList = list.stream().collect(Collectors.groupingBy(item -> (String) item.get("fraction"))) //分数  分组.entrySet().stream().sorted((s1, s2) -> {     //排序int a1 = Integer.parseInt(s1.getKey());int a2 = Integer.parseInt(s2.getKey());return (a1 < a2) ? 1 : -1;}).collect(Collectors.toList());//同分数据根据sort排序 for (Map.Entry>> entry : tempList) {List> intemList = entry.getValue();entry.setValue(intemList.stream().sorted((s1, s2) -> {  //子排序字段int a1 = Integer.parseInt((String) s1.get("sort"));int a2 = Integer.parseInt((String) s2.get("sort"));return (a1 > a2) ? 1 : -1;}).collect(Collectors.toList()));}//创建最后的排序对象List> resList = new ArrayList<>();int index=1;for (Map.Entry>> entry : tempList) {List> itemList = entry.getValue();for(Map itemMap:itemList){itemMap.put("index",index);resList.add(itemMap);}index=index+itemList.size();}resList.forEach(System.out::println);

五、运行结果截图

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部