Here you can find the source of sortNode(DefaultMutableTreeNode parent, Comparator
public static void sortNode(DefaultMutableTreeNode parent, Comparator<DefaultMutableTreeNode> comparator)
//package com.java2s; import java.util.Comparator; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.MutableTreeNode; public class Main { public static void sortNode(DefaultMutableTreeNode parent, Comparator<DefaultMutableTreeNode> comparator) { int n = parent.getChildCount(); for (int i = 0; i < n - 1; i++) { int min = i; for (int j = i + 1; j < n; j++) { if (comparator.compare((DefaultMutableTreeNode) parent.getChildAt(min), (DefaultMutableTreeNode) parent.getChildAt(j)) > 0) { min = j;//ww w . ja va2 s . com } } if (i != min) { MutableTreeNode a = (MutableTreeNode) parent.getChildAt(i); MutableTreeNode b = (MutableTreeNode) parent.getChildAt(min); parent.insert(b, i); parent.insert(a, min); } } } }