## 如何实现查询结果的树排序？

1、组织机构表 ID,NAME,PID,SORT_ORDER

2、用户表 ID,NAME,DEPTID,SORT_ORDER

3.现有数据显示的树状结构如图：

• 小旋风柴进
2019-07-17 18:59:15
``````import contact.Dept;
import contact.Emp;
import contact.EmpComparator;

import java.util.*;

public class Program {
private static Map<Integer, Dept> deptMap;
private static Map<Integer, Emp>  empMap;

private static void init() {
Dept newDept = null;
Emp  newEmp  = null;

deptMap = new HashMap<>();
empMap  = new HashMap<>();

newDept = new Dept(1, "公司",   0, 99); deptMap.put(newDept.Id, newDept);
newDept = new Dept(2, "技术部", 1, 90); deptMap.put(newDept.Id, newDept);
newDept = new Dept(3, "市场部", 1, 80); deptMap.put(newDept.Id, newDept);
newDept = new Dept(4, "测试组", 2, 90); deptMap.put(newDept.Id, newDept);
newDept = new Dept(5, "研发组", 2, 95); deptMap.put(newDept.Id, newDept);

newEmp = new Emp(1,  "老板",         1, 99); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(2,  "技术部经理",   2, 90); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(3,  "技术部副经理", 2, 89); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(4,  "市场部经理",   3, 90); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(5,  "测试组长",     4, 90); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(6,  "研发组长",     5, 90); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(7,  "测试组员1",    4, 80); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(8,  "测试组员2",    4, 50); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(9,  "研发组员1",    5, 80); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(10, "研发组员2",    5, 70); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(11, "秘书",         1, 90); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(12, "市场专员1",    3, 50); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(13, "市场专员2",    3, 50); empMap.put(newEmp.Id, newEmp);
}

private static List<Emp> getContacts() {
List<Emp> result = new ArrayList<>(empMap.values());
Collections.sort(result, new EmpComparator(deptMap));
return result;
}

public static void main(String[] args) {
init();

for (Emp emp : getContacts()) {
System.out.println(emp);
}
}
}import contact.Dept;
import contact.Emp;
import contact.EmpComparator;

import java.util.*;

public class Program {
private static Map<Integer, Dept> deptMap;
private static Map<Integer, Emp>  empMap;

private static void init() {
Dept newDept = null;
Emp  newEmp  = null;

deptMap = new HashMap<>();
empMap  = new HashMap<>();

newDept = new Dept(1, "公司",   0, 99); deptMap.put(newDept.Id, newDept);
newDept = new Dept(2, "技术部", 1, 90); deptMap.put(newDept.Id, newDept);
newDept = new Dept(3, "市场部", 1, 80); deptMap.put(newDept.Id, newDept);
newDept = new Dept(4, "测试组", 2, 90); deptMap.put(newDept.Id, newDept);
newDept = new Dept(5, "研发组", 2, 95); deptMap.put(newDept.Id, newDept);

newEmp = new Emp(1,  "老板",         1, 99); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(2,  "技术部经理",   2, 90); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(3,  "技术部副经理", 2, 89); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(4,  "市场部经理",   3, 90); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(5,  "测试组长",     4, 90); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(6,  "研发组长",     5, 90); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(7,  "测试组员1",    4, 80); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(8,  "测试组员2",    4, 50); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(9,  "研发组员1",    5, 80); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(10, "研发组员2",    5, 70); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(11, "秘书",         1, 90); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(12, "市场专员1",    3, 50); empMap.put(newEmp.Id, newEmp);
newEmp = new Emp(13, "市场专员2",    3, 50); empMap.put(newEmp.Id, newEmp);
}

private static List<Emp> getContacts() {
List<Emp> result = new ArrayList<>(empMap.values());
Collections.sort(result, new EmpComparator(deptMap));
return result;
}

public static void main(String[] args) {
init();

for (Emp emp : getContacts()) {
System.out.println(emp);
}
}
}``````
0 0
+ 订阅