• title: JTreeのソート tags: [JTree, TreeNode, Comparator] author: aterai pubdate: 2009-05-04T16:26:55+09:00 description: 葉ノードより親ノード優先でノード名を比較するComparatorを作成して、JTreeをソートします。

概要

葉ノードより親ノード優先でノード名を比較するComparatorを作成して、JTreeをソートします。

サンプルコード

public static void sortTree(DefaultMutableTreeNode root) {
  Enumeration e = root.depthFirstEnumeration();
  while (e.hasMoreElements()) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
    if (!node.isLeaf()) {
      sort2(node);   //selection sort
      //sort3(node); //JDK 1.6.0: iterative merge sort
      //sort3(node); //JDK 1.7.0: TimSort
    }
  }
}
public static Comparator<DefaultMutableTreeNode> tnc = new Comparator<DefaultMutableTreeNode>() {
  @Override public int compare(DefaultMutableTreeNode a, DefaultMutableTreeNode b) {
    //...
  }
};
View in GitHub: Java, Kotlin
//selection sort
public static void sort2(DefaultMutableTreeNode parent) {
  int n = parent.getChildCount();
  for (int i = 0; i < n - 1; i++) {
    int min = i;
    for (int j = i + 1; j < n; j++) {
      if (tnc.compare((DefaultMutableTreeNode) parent.getChildAt(min),
                      (DefaultMutableTreeNode) parent.getChildAt(j)) > 0) {
        min = j;
      }
    }
    if (i != min) {
      MutableTreeNode a = (MutableTreeNode) parent.getChildAt(i);
      MutableTreeNode b = (MutableTreeNode) parent.getChildAt(min);
      parent.insert(b, i);
      parent.insert(a, min);
    }
  }
}
public static void sort3(DefaultMutableTreeNode parent) {
  int n = parent.getChildCount();
  //@SuppressWarnings("unchecked")
  //Enumeration<DefaultMutableTreeNode> e = parent.children();
  //ArrayList<DefaultMutableTreeNode> children = Collections.list(e);
  List<DefaultMutableTreeNode> children = new ArrayList<>(n);
  for (int i = 0; i < n; i++) {
    children.add((DefaultMutableTreeNode)parent.getChildAt(i));
  }
  Collections.sort(children, tnc); //using Arrays.sort(...)
  parent.removeAllChildren();
  for (MutableTreeNode node: children) {
    parent.add(node);
  }
}

解説

上記のサンプルでは、チェックボックスをクリックするとルートのDefaultMutableTreeNodeからdeep copyでクローンを作成し、各親ノードを深さ優先で探索して、昇順ソートしています。

元のソート無しの状態に戻す場合は、DefaultTreeModelを作成し直しています。


  • DefaultMutableTreeNodeの比較は、Comparator<DefaultMutableTreeNode>#compareをオーバーライドし、節ノードが葉ノードより上、かつgetUserObject().toString()で生成した文字列の大文字小文字を無視している
public static Comparator<DefaultMutableTreeNode> tnc = new Comparator<DefaultMutableTreeNode>() {
  @Override public int compare(DefaultMutableTreeNode a, DefaultMutableTreeNode b) {
    if (a.isLeaf() && !b.isLeaf()) {
      return 1;
    } else if (!a.isLeaf() && b.isLeaf()) {
      return -1;
    } else {
      String sa = a.getUserObject().toString();
      String sb = b.getUserObject().toString();
      return sa.compareToIgnoreCase(sb);
    }
  }
};
Comparator<String> sci = Comparator.comparingInt(String::length)
                                   .thenComparing(String.CASE_INSENSITIVE_ORDER);
Comparator<DefaultMutableTreeNode> tnc = Comparator.comparing(DefaultMutableTreeNode::isLeaf)
                                   .thenComparing(n -> n.getUserObject().toString(), sci);
//                                   .thenComparing(n -> n.getUserObject().toString().toLowerCase());

参考リンク

コメント