zTree菜单的排序

zTree菜单的排序,查询出来为所有的菜单项
循环遍历取的时候,为了保证它的顺序,所以先做个排序
只要保证相同父节点下的,所有的子节点,都是有序的,取的时候,从根节点开始得到的菜单就是有序的

private static List convertToOrder(List resources){
    // ParentId集合,取出所有的parentid
    List setParentId=new ArrayList<>();
    for(int i=0;iget(i).getParentId().toString();
        if(!setParentId.contains(ParentId)){
            setParentId.add(ParentId);
        }
    }
    // ParentId集合,存放到Map中
    Map> arraySource=new HashMap>();
    for(int i=0;i systemResourceList=new ArrayList<>();
        arraySource.put(setParentId.get(i).toString(),systemResourceList);
    }
    // 根据Key,添加list,parentid为key,list为所有的子节点
    for(int i=0;iif(arraySource.containsKey(resources.get(i).getParentId().toString())){
            arraySource.get(resources.get(i).getParentId().toString()).add(resources.get(i));
        }
    }
    // 根据key,获取list排序
    List resourceList=new ArrayList<>();
    for(int i=0;i systemResourceList=new ArrayList<>();
        if (arraySource.get(setParentId.get(i)).size()>0){
            systemResourceList=orderSystemResourceList(arraySource.get(setParentId.get(i)));
        }
        resourceList.addAll(systemResourceList);
    }
    return resourceList;
}

// list排序,同一个parentID下面的,所有子节点排序
private static List orderSystemResourceList(List systemResourceList){
    // Weight集合,放到Map中,key为子节点的权重,value为子节点
    Map resourceMap=new HashedMap();
    for(int i=0;inew SystemResource();
        resourceMap.put(systemResourceList.get(i).getWeight().toString(),systemResourceList.get(i));
    }
    // Weight集合,权重集合
    int arrayInt[]=new int[systemResourceList.size()];
    for(int i=0;iget(i).getWeight();
    }
    // Weight集合,权重排序
    for (int i = 0; i < arrayInt.length; i++) {
        for (int j = i+1; j < arrayInt.length; j++) {
            if (arrayInt[i] < arrayInt[j]) {
                int temp = arrayInt[i];
                arrayInt[i] = arrayInt[j];
                arrayInt[j] = temp;
            }
        }
    }
    // 根据有序的key,获取value子节点
    List systemResourceListByWeight=new ArrayList<>();
    for (int i=0;iget(String.valueOf(arrayInt[i])));
    }
    return systemResourceListByWeight;
}

上述代码,是用不到的
直接在查询的时候,排序即可
select * from SYS_RESOURCE ORDER BY PARENT_ID,WEIGHT

你可能感兴趣的:(————Java,EE)