您的位置:首页 > Web前端 > Node.js

Node

2016-05-18 19:30 435 查看
import java.text.Collator;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.Locale;

import net.sf.json.JSONObject;

/**

 * 节点类

 */

public class Node {
/**
* 节点编号
*/
private String id;
/**
* 节点名称
*/
private String name;
/**
* 节点内容
*/
private String context;
/**
* 类型
*/
private String type;
/**
* 父节点编号
*/
private String parentId;
/**
* 孩子节点列表
*/
private List<Node> children;
/**
* 排序字段
*/
private String sort;

public Node() {
super();
}

public Node(String id, String name) {
super();
this.id = id;
this.name = name;
}

public Node(String id, String name, String type) {
super();
this.id = id;
this.name = name;
this.type = type;
}

public Node(String id, String name, String type, String parentId) {
super();
this.id = id;
this.name = name;
this.type = type;
this.parentId = parentId;
}

public Node(String id, String name, String type, String parentId, String sort) {
super();
this.id = id;
this.name = name;
this.type = type;
this.parentId = parentId;
this.sort = sort;
}

public Node(String id, String name, String type, String parentId, List<Node> children, String sort) {
super();
this.id = id;
this.name = name;
this.type = type;
this.parentId = parentId;
this.children = children;
this.sort = sort;
}

/**
* 添加孩子节点
*/
public void addChild(Node node) {
if (this.children == null)
this.children = new ArrayList<Node>();
this.children.add(node);
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getContext() {
return context;
}

public void setContext(String context) {
this.context = context;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getParentId() {
return parentId;
}

public void setParentId(String parentId) {
this.parentId = parentId;
}

public List<Node> getChildren() {
return children;
}

public void setChildren(List<Node> children) {
this.children = children;
}

public String getSort() {
return sort;
}

public void setSort(String sort) {
this.sort = sort;
}

public static void sortNodeByInt(Node node) {
if (node.getChildren() != null && node.getChildren().size() > 0) {
sortNodeListByInt(node.getChildren());
}
}

public static void sortNodeListByInt(List<Node> nodeList) {
if (nodeList != null && nodeList.size() > 0) {
Collections.sort(nodeList, new IntegerComparator());

for (Node node : nodeList) {
if (node.getChildren() != null && node.getChildren().size() > 0) {
sortNodeListByInt(node.getChildren());
}
}
}
}

public static void sortNodeByString(Node node) {
if (node.getChild
4000
ren() != null && node.getChildren().size() > 0) {
sortNodeListByString(node.getChildren());
}
}

public static void sortNodeListByString(List<Node> nodeList) {
if (nodeList != null && nodeList.size() > 0) {
Collections.sort(nodeList, new StringComparator());

for (Node node : nodeList) {
if (node.getChildren() != null && node.getChildren().size() > 0) {
sortNodeListByString(node.getChildren());
}
}
}
}

public static void main(String[] args) {
Node node1 = new Node("1", "name1");
Node node2 = new Node("2", "name2");
node2.setSort("03");
Node node3 = new Node("3", "name3");
node3.setSort("2");
Node node4 = new Node("4", "name4");
node1.addChild(node2);
node1.addChild(node3);
node2.addChild(node4);

// sortNodeByInt(node1);
sortNodeByString(node1);

System.out.println(JSONObject.fromObject(node1));
}

}

class IntegerComparator implements Comparator<Node> {
public int compare(Node node1, Node node2) {
int i = Integer.parseInt(node1.getSort());
int j = Integer.parseInt(node2.getSort());
return (i < j ? -1 : (i == j ? 0 : 1));
}

}

class StringComparator implements Comparator<Node> {
public int compare(Node node1, Node node2) {
return Collator.getInstance(Locale.CHINA).compare(node1.getSort(), node2.getSort());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: