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());
}
}
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());
}
}
相关文章推荐
- node.js 安装和 配置Sublime Text2的Nodejs
- childNodes和Children的区别
- nodejs window下安装与配置淘宝镜像
- 配置 Sublime Text 开发node.js和js-beautify-sublime(代码格式化)
- Node.js EventEmitter事件发射器
- node-webkit:开发桌面+WEB混合型应用的神器
- Node.js刷新session过期时间的实现方法推荐
- 【leetcode】237. Delete Node in a Linked List
- 关于nodejs iconv的编码无效 说明
- 利用Express搭建空白工程
- 使用WebStorm调试node工程
- Node.js返回JSONP详解
- 简单HDFS使用Journalnode HA部署
- 基于superagent 与 cheerio 的node简单爬虫
- Error Handling in Node.js
- NodeJs处理post请求
- NodeJs的阻塞和非阻塞
- NodeJs如何处理请求的路由
- NodeJs获取请求参数的方法(get方式)
- Node.js中的setImmediate()与process.nextTick