---
category: swing
folder: SortTree
title: JTreeのソート
tags: [JTree, TreeNode, Comparator]
author: aterai
pubdate: 2009-05-04T16:26:55+09:00
description: JTreeを葉ノードより親ノード優先でノード名を比較するComparatorを使用してソートします。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTThR240sI/AAAAAAAAAkg/h3mIbDu9xa4/s800/SortTree.png
hreflang:
href: https://java-swing-tips.blogspot.com/2013/09/how-to-sort-jtree-nodes.html
lang: en
---
* Summary [#summary]
`JTree`を葉ノードより親ノード優先でノード名を比較する`Comparator`を使用してソートします。
#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTThR240sI/AAAAAAAAAkg/h3mIbDu9xa4/s800/SortTree.png)
* Source Code Examples [#sourcecode]
#code(link){{
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) {
// ...
}
};
}}
#code{{
// 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);
}
}
}
}}
#code{{
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);
}
}
}}
* Description [#explanation]
* Description [#description]
上記のサンプルでは、チェックボックスがクリックされると以下の手順でソートを実行します。
- `DefaultTreeModel`から`deep copy`でクローンを作成
- クローンされたモデルのルート`DefaultMutableTreeNode`を深さ優先で探索することで昇順ソート
- ソート済みのモデルを`JTree`に設定
-- ソート無しの状態に戻す場合は、別途保存してある元の`DefaultTreeModel`を`JTree`に設定
----
- `DefaultMutableTreeNode`の比較は`Comparator<DefaultMutableTreeNode>#compare`をオーバーライドし、節ノードが葉ノードより上、かつ`getUserObject().toString()`で生成した文字列の大文字小文字を無視している
#code{{
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);
}
}
};
}}
- `JDK 1.8.0`以降の場合、この`Comparator`を以下のように簡単に作成できる
-- 参考: [http://d.hatena.ne.jp/yohhoy/20141007/p1 Comparator with ラムダ式 - yohhoyの日記]
-- 参考: [https://docs.oracle.com/javase/jp/8/docs/api/java/util/Comparator.html#thenComparing-java.util.Comparator- Comparator#thenComparing(...) (Java Platform SE 8)]
#code{{
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());
}}
----
- `sort3`で使用している`Collections.sort(...)`は内部で`Arrays.sort(T[], Comparator<? super T>)`を使用しているので、`JDK 1.6.0`と`JDK 1.7.0`以降でソートアルゴリズムが異なる
-- 参考: [https://stackoverflow.com/questions/4018332/is-java-7-using-tim-sort-for-the-method-arrays-sort Is Java 7 using Tim Sort for the Method Arrays.Sort? - Stack Overflow]
- `JDK 1.6.0`
#code{{
// Arrays.sort(T[] a, Comparator<? super T> c)
public static <T> void sort(T[] a, Comparator<? super T> c) {
T[] aux = (T[]) a.clone();
if (c == null) {
mergeSort(aux, a, 0, a.length, 0);
} else {
mergeSort(aux, a, 0, a.length, 0, c);
}
}
}}
- `JDK 1.7.0`
#code{{
// Arrays.sort(T[] a, Comparator<? super T> c)
public static <T> void sort(T[] a, Comparator<? super T> c) {
if (c == null) {
sort(a);
} else {
if (LegacyMergeSort.userRequested) {
legacyMergeSort(a, c);
} else {
TimSort.sort(a, 0, a.length, c, null, 0, 0);
}
}
}
}}
* Reference [#reference]
- [https://community.oracle.com/thread/1355435 Swing - How to sort jTree Nodes]
-- 以下のコメントにバグの指摘あり
//-- `#6`のHamedさんの投稿 (以下のコメントにバグの指摘あり)
- [[JTreeのノードを走査する>Swing/TraverseAllNodes]]
-- `JTree`ノードの深さ優先探索などについて
- [[JComboBoxのモデルとしてenumを使用する>Swing/SortingAnimations]]
-- 各種ソートアルゴリズムのサンプル
- [[JTableでファイルとディレクトリを別々にソート>Swing/FileDirectoryComparator]]
-- ディレクトリが先になる比較について
* Comment [#comment]
#comment
- ソースにバグあります。 `root.insert(prevNode, i);`の後に`i--;`を入れる必要あり -- &user(a); &new{2013-07-02 (火) 15:54:09};
-- ご指摘ありがとうございます。たしかに`i--;break`などがないと、入れ替えられてソートされないノードが出来てしまいますね。効率も悪いので、深さ優先で探索した親ノードから別の方法でソートするように変更した方がいいかもしれません。 %%しばらくテストしてこのサンプルは修正したいと思います。%% 修正しました。 -- &user(aterai); &new{2013-07-03 (水) 13:11:51};
#comment