导航树java排序算法

import java.io.Serializable;

/**
 * 菜单bean
 * @author 雾非雾
 */
public class Menu implements Serializable{

	private static final long serialVersionUID = -2551261287753455586L;

	public Integer id;
	
	public Integer parentId; //父id

	public String name; //菜单名称

	public Integer sort; //菜单顺序
	
	public Integer hit; //菜单点击量

	public String createDate;
	
	public String updateDate;
	
	public String picPath;
	
	public String isShow; //是否显示
	
	public String link; //菜单链接地址
	
	public Integer level; //菜单级别
	
	public Menu parentMenu;

	public Integer getId() {
		return id;
	}

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

	public Integer getParentId() {
		return parentId;
	}

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

	public String getName() {
		return name;
	}

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

	public Integer getSort() {
		if(null==sort){
			return 0;
		}
		return sort;
	}

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

	public String getCreateDate() {
		return createDate;
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

	public String getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(String updateDate) {
		this.updateDate = updateDate;
	}

	public String getPicPath() {
		return picPath;
	}

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}

	public Integer getHit() {
		return hit;
	}

	public void setHit(Integer hit) {
		this.hit = hit;
	}

	public String getIsShow() {
		return isShow;
	}

	public void setIsShow(String isShow) {
		this.isShow = isShow;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public Menu getParentMenu() {
		return parentMenu;
	}

	public void setParentMenu(Menu parentMenu) {
		this.parentMenu = parentMenu;
	}
	
	
	@Override
	public String toString() {
		StringBuffer s = new StringBuffer().
			append("id=").
			append(id).
			append(",name=").
			append(name).
			append(",parentId=").
			append(parentId).
			append(",level=").
			append(level);
		return s.toString();
	}

}

import java.util.Comparator;
//比较器
public class MenuComparator implements Comparator<Menu>{

    public int compare(Menu m1, Menu m2) {
		if(m1.level==1 && m2.level==1){ //都是一级menu,按sort排序
			if(m1.getSort()>m2.getSort()){
				return 1;
			}else if(m1.getSort()<m2.getSort()){
				return -1;
			}
			return 0;
		}
		if(m1.parentId==m2.parentId){ //如果当前对象和比较对象都有同一个父亲
			if(m1.getSort()>m2.getSort()){
				return 1;
			}else if(m1.getSort()<m2.getSort()){
				return -1;
			}
			return 0;
		}
		if(m1.id==m2.parentId){ //如果当前是比较对象的父menu
			return -1;
		}
		if(m1.parentId==m2.id){ //如果比较对象是否当前对象的父menu
			return 1;
		}
		if(m1.level<m2.level){
			Menu parent = m2.parentMenu;
			while(parent!=null && parent.level>0){ //如果当前对象是比较对象的父父辈...
				if(m1.id==parent.id){
					return -1;
				}
				parent = parent.getParentMenu();
			}
		}
		if(m1.level>m2.level){
			Menu parent = m1.parentMenu;
			while(parent!=null && parent.level>0){ //如果当前对象是比较对象的子子辈...
				if(m2.id==parent.id){
					return 1;
				}
				parent = parent.getParentMenu();
			}
		}
		
		//如果不存在父子关系,则比较一级对象的顺序
		Menu menu1 = m1;
		if(m1.level!=1){
			Menu parent = m1.parentMenu;
			while(parent!=null && parent.level>0){ 
				if(parent.level==1){ //向上找父,一直找到一级menu
					menu1 = parent;
					break;
				}
				parent = parent.getParentMenu();
			}
		}
		Menu menu2 = m2;
		if(m2.level!=1){
			Menu parent = m2.parentMenu; //向上找父,一直找到一级menu
			while(parent!=null && parent.level>0){ 
				if(parent.level==1){ 
					menu2 = parent;
					break;
				}
				parent = parent.getParentMenu();
			}
		}
		if(menu1.getSort()>menu2.getSort()){
			return 1;
		}else if(menu1.getSort()<menu2.getSort()){
			return -1;
		}
		return 0;
    }
    
}

//排序测试
		List<Menu> list = this.menuService.getMenuList();
		Map<Integer,Menu> map = new HashMap<Integer,Menu>();
		for(Integer i=0;i<list.size();i++){
			map.put(list.get(i).getId(), list.get(i));
		}
		
		for(Integer i=0;i<list.size();i++){
			Menu item = list.get(i);
			if(null!=item.getParentId()){
				item.setParentMenu(map.get(item.getParentId()));
			}
		}
		
		Collections.sort(list,new MenuComparator());



你可能感兴趣的:(导航树java排序算法)