编程语言
首页 > 编程语言> > 同级目录排序问题,置顶、置底、上移、下移 Java

同级目录排序问题,置顶、置底、上移、下移 Java

作者:互联网

在这里插入图片描述

参考代码实现如下(还需优化):


```java
public class SortUtils {

    // 当前数据节点
    private BusinessSystemFunctionData current;
    // 兄弟节点数据
    private List<BusinessSystemFunctionData> brother;
    //以当前节点分割,当前之前的数据
    private List<BusinessSystemFunctionData> beforeList;
    //以当前节点分割,当前之后的数据
    private List<BusinessSystemFunctionData> afterList;
    // 当前节点的下标
    private int currintIndex;
    // 兄弟节点是否包含当前节点
    private boolean constan;


    public SortUtils(BusinessSystemFunctionData businessSystemFunctionData, List<BusinessSystemFunctionData> list) {
        this.current = businessSystemFunctionData;
        this.brother = list;
        check();
    }

    /**
     * 以当前节点,把兄弟数据分为两部分,分为前后,方便置顶和置顶操作
     */
    public void split() {
        this.beforeList = this.brother.subList(0, currintIndex);
        this.afterList = this.brother.subList(currintIndex + 1, this.brother.size());

    }

    /**
     * 校验数据的合法性
     */
    private void check() {
        if (null == this.current || null == this.brother) {
            throw new IllegalArgumentException("非法参数,请初始化参数");
        }
        for (int i = 0; i < brother.size(); i++) {
            if (this.current.getId().equals(brother.get(i).getId())) {
                currintIndex = i;
                constan = true;
                break;
            }
        }
        if (!constan || currintIndex >= this.brother.size()) {
            throw new IllegalArgumentException("非法参数,初始化参数,有误,兄弟节点不包含当前节点");
        }
    }

    /**
     * 置顶
     *
     * @return
     */
    public List<BusinessSystemFunctionData> toTop() {
        List<BusinessSystemFunctionData> list = new ArrayList<>(brother.size());
        list.add(this.current);
        list.addAll(beforeList);
        list.addAll(afterList);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSort((i + 1));
        }
        return list;
    }

    /**
     * 置底
     *
     * @return
     */
    public List<BusinessSystemFunctionData> toBottom() {
        List<BusinessSystemFunctionData> list = new ArrayList<>(brother.size());
        list.addAll(beforeList);
        list.addAll(afterList);
        list.add(this.current);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSort((i + 1));
        }
        return list;
    }

    /**
     * 上移
     *
     * @return
     */
    public List<BusinessSystemFunctionData> toUp() {
        if (currintIndex > 0) {
            BusinessSystemFunctionData businessSystemFunctionData = this.brother.get(currintIndex - 1);
            this.brother.set(currintIndex - 1, current);
            this.brother.set(currintIndex, businessSystemFunctionData);
        }
        for (int i = 0; i < this.brother.size(); i++) {
            this.brother.get(i).setSort((i + 1));
        }
        return this.brother;
    }

    /**
     * 下移动
     *
     * @return
     */
    public List<BusinessSystemFunctionData> toDown() {
        if (currintIndex < brother.size() - 1) {
            BusinessSystemFunctionData businessSystemFunctionData = this.brother.get(currintIndex + 1);
            this.brother.set(currintIndex + 1, current);
            this.brother.set(currintIndex, businessSystemFunctionData);
        }
        for (int i = 0; i < this.brother.size(); i++) {
            this.brother.get(i).setSort((i + 1));
        }
        return this.brother;
    }
}


标签:Java,List,list,brother,current,currintIndex,置底,置顶,size
来源: https://blog.csdn.net/qq_30801117/article/details/120337760