排序 Parent-Children 关系

Sorting Parent-Children relations

我在为我的 class Page:

找出一个简单的 compareTo-method 时遇到了一些问题

我喜欢按以下方式排序:

  1. 父母A
  2. A1 的子父
  3. A2 的子父
  4. 父B
  5. 父C
  6. C1 的子父
  7. C2 的子父母

我的 compareTo 需要怎么看才能解决这个问题?我尝试了以下,目前只能正确排序相同级别。

我的做法:

@Override
public int compareTo(@NonNull Object o) {
    int compare = 0;
    if (o instanceof Page) {
        Page other = (Page) o;
        if (other.parent != null) {
            compare = this.compareTo(other.parent);
        } else if (parent != null) {
            compare = this.parent.compareTo(other);
        }
        if (compare == 0) {
            compare = Integers.compareTo(this.order, other.order);
        }
    }
    return compare;
}

编辑:我的Page-Class:

class Page{
    int order;
    Page parent;
}

您可能有一种检测页面深度的方法

解决方法是这样的:

public class Page {
    private int order;
    private Page parent;

    public Page() {
    }

    public Page(int order, Page parent) {
        this.order = order;
        this.parent = parent;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public Page getParent() {
        return parent;
    }

    public void setParent(Page parent) {
        this.parent = parent;
    }


    public int compareTo(Object o){
        int compare = 0;
        if (o instanceof Page) {
            Page other = (Page) o;
            if(this.depth() == other.depth()){
                if(Objects.equals(this.getParent(), other.getParent())){
                    return Integer.compare( other.getOrder(),this.getOrder());
                }
                else{
                    return this.getParent().compareTo(other.getParent());
                }
            }
            else{
                return Integer.compare(other.depth() , this.depth());
            }
        }
        return compare;
    }

    public int depth(){
        if(this.parent == null)return 0;
        else return 1+this.parent.depth();
    }

    public static void main(String[] args){
        Page a = new Page(1, null);
        Page a_1 = new Page(1, a);
        Page a_2 = new Page(2, a);
        Page a_1_1 = new Page(1, a_1);
        Page a_1_2 = new Page(2, a_1);
        Page a_2_1 = new Page(1, a_2);
        Page a_2_2 = new Page(2, a_2);

        System.out.println(a_1.compareTo(a_1_1));
        //1 because a_1 has higher depth
        System.out.println(a_1.compareTo(a_2));
        //1 because a_1 has smaller order
        System.out.println(a_1_2.compareTo(a_1_1));
        //-1 because a_1_2 has higher order
        System.out.println(a_2_1.compareTo(a_1_1));
        //-1 because a_2_1 parent is a_2 and a_1_1 parent is a_1
    }
}

无需进行复杂的计算,您可以简单地依赖 order 链。我使用 String 来存储每个 "node":

的链
class Page implements Comparable<Page> {
    String name;
    int order;
    Page parent;
    String key;

    Page() {
        name = "root";
        order  = 0;
        parent = null;
        key    = "";
    }

    Page(String name, int order, Page parent) {
        this.name   = name;
        this.order  = order;
        this.parent = parent;
        key = parent.key + (char)order;
    }

    @Override
    public int compareTo(Page o) {
        return key.compareTo(o.key);
    }

    @Override
    public String toString() {
        return name;
    }
}

public static void main(String[] args) {
    Page root = new Page();

    Page b    = new Page("b"  , 2, root);
    Page b1   = new Page("b.1", 1, b);
    Page b3   = new Page("b.3", 3, b);
    Page b2   = new Page("b.2", 2, b);

    Page a    = new Page("a"  , 1, root);
    Page a2   = new Page("a.2", 2, a);
    Page a1   = new Page("a.1", 1, a);

    List<Page> pages = Arrays.asList(root, a, a1, a2, b, b1, b2, b3);
    System.out.println(pages);

    Collections.shuffle(pages);
    System.out.println(pages);

    Collections.sort(pages);
    System.out.println(pages);
}

这里的技巧是使用 String 作为可排序的 short 数组。该数组是 order 的组合,构成从根到节点的路径。 root[] 的 key/id 和 a 来自 root 的 child 和 order 1 作为 key/id [1].

这里是 key/id 矩阵:

┌──────┬────────┬───────┬────────────┬─────┐
│ Page │ Parent │ Order │ Parent Key │ Key │
├──────┼────────┼───────┼────────────┼─────┤
│ root │ -      │ -     │ -          │     │
│ a    │ root   │ 1     │            │ 1   │
│ a.1  │ a      │ 1     │ 1          │ 1 1 │
│ a.2  │ a      │ 2     │ 1          │ 1 2 │
│ b    │ root   │ 2     │            │ 2   │
│ b.1  │ b      │ 1     │ 2          │ 2 1 │
│ b.2  │ b      │ 2     │ 2          │ 2 2 │
│ b.3  │ b      │ 3     │ 2          │ 2 3 │
└──────┴────────┴───────┴────────────┴─────┘

这是来自另一棵树的示例(括号中的关键字)

root      (     )
├ z       (1    )
│ ├ a     (1 1  )
│ │ └ y   (1 1 1)
│ └ b     (1 2  )
│   └ x   (1 2 1)
└ c       (2    )
  ├ w     (2 1  )
  └ d     (2 2  )