Node

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.getChildren() != 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());
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值