list转tree结构时如何加上排序?
Entity
@Datapublic class MenuNode {
private Integer id;
private String name;
private String menuCode;
private String parentCode;
private String path;
private Integer sort;
private List<MenuNode> children;
}
Controller
@RequestMapping("/trees")public List<MenuNode> trees() {
// return service.trees();
return listToTree(service.trees());
}
// list转tree结构
private List<MenuNode> listToTree(List<MenuNode> list) {
List<MenuNode> tree = new ArrayList<>();
for (MenuNode menu : list) {
//找到根节点
if (menu.getParentCode().length() == 0) {
tree.add(menu);
}
//再次遍历list,找到子节点
List<MenuNode> children = new ArrayList<>();
for (MenuNode node : list) {
if (node.getParentCode().equals(menu.getMenuCode())) {
children.add(node);
}
}
menu.setChildren(children);
}
return tree;
}
id | menu_code | parent_code | name | path | sort |
---|---|---|---|---|---|
1 | ME00001 | 菜单1 | /a | 3 | |
2 | ME00002 | 菜单2 | /b | 2 | |
3 | ME00003 | 菜单3 | /c | 1 | |
4 | ME00004 | ME00001 | 菜单11 | /a/i | 4 |
5 | ME00005 | ME00001 | 菜单12 | /a/j | 0 |
6 | ME00006 | ME00004 | 菜单111 | /a/i/x | 0 |
7 | ME00007 | ME00004 | 菜单112 | /a/i/y | 0 |
结果没排序
sort
数据库默认值0,排序不同层级的要设置成唯一吗?
再写一个方法,递归遍历数组排序?
[ {
"id": 1,
"name": "菜单1",
"menuCode": "ME00001",
"parentCode": "",
"path": "/a",
"sort": 1,
"children": [
{
"id": 4,
"name": "菜单11",
"menuCode": "ME00004",
"parentCode": "ME00001",
"path": "/a/i",
"sort": 4,
"children": [
{
"id": 6,
"name": "菜单111",
"menuCode": "ME00006",
"parentCode": "ME00004",
"path": "/a/i/x",
"sort": 0,
"children": []
},
{
"id": 7,
"name": "菜单112",
"menuCode": "ME00007",
"parentCode": "ME00004",
"path": "/a/i/y",
"sort": 0,
"children": []
}
]
},
{
"id": 5,
"name": "菜单12",
"menuCode": "ME00005",
"parentCode": "ME00001",
"path": "/a/j",
"sort": 0,
"children": []
}
]
},
{
"id": 2,
"name": "菜单2",
"menuCode": "ME00002",
"parentCode": "",
"path": "/b",
"sort": 2,
"children": []
},
{
"id": 3,
"name": "菜单3",
"menuCode": "ME00003",
"parentCode": "",
"path": "/c",
"sort": 3,
"children": []
}
]
回答:
楼主可以用list的stream流来进行转换
例如:
//查询数据库所有的数据List<AllSpaceVo> spaceInfoAllVos = mapperFacade.mapAsList(list, AllSpaceVo.class);
//查询出一级空间
List<AllSpaceVo> collect = spaceInfoAllVos.stream()
.filter(info -> {
if (id == null) {
return info.getSpaceParentId().equals(0L);
} else {
return info.getSpaceParentId().equals(id);
}
})
.map(menu -> {
menu.setChildSpaces(getChild(menu, spaceInfoAllVos));
return menu;
})
.sorted(Comparator.comparing(AllSpaceVo::getDefOrder)
.thenComparing(AllSpaceVo::getCreateTime, Comparator.reverseOrder()))
.collect(Collectors.toList());
private List<AllSpaceVo> getChild(AllSpaceVo root, List<AllSpaceVo> all) { return all.stream()
.filter((allSpaceVo -> root.getId().equals(allSpaceVo.getSpaceParentId())))
.map(menu -> {
menu.setChildSpaces(getChild(menu, all));
return menu;
})
.sorted(Comparator.comparing(AllSpaceVo::getDefOrder)
.thenComparing(AllSpaceVo::getCreateTime, Comparator.reverseOrder()))
.collect(Collectors.toList());
}
用sorted进行排序
回答:
是要根据 sort 排序?看 json 结果这不是有序的?你期望的返回结果是什么?
以上是 list转tree结构时如何加上排序? 的全部内容, 来源链接: utcz.com/p/944712.html