void () { DefaultMutableTreeNode root = new DefaultMutableTreeNode("root"); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("2"); root.add(node2); DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("1"); root.add(node1); DefaultMutableTreeNode node1_2 = new DefaultMutableTreeNode("1_2"); node1.add(node1_2); DefaultMutableTreeNode node1_1 = new DefaultMutableTreeNode("1_1"); node1.add(node1_1); DefaultTreeModel model = new DefaultTreeModel(root); TreeUtil.sort(model, Comparator.comparing(Object::toString)); assertEquals(node1, root.getChildAt(0)); assertEquals(node2, root.getChildAt(1)); assertEquals(node1_1, node1.getChildAt(0)); assertEquals(node1_2, node1.getChildAt(1)); TreeUtil.sort(model, (o1, o2) -> { TreeNode n1 = (TreeNode) o1; TreeNode n2 = (TreeNode) o2; return n1.getChildCount() - n2.getChildCount(); }); assertEquals(node2, root.getChildAt(0)); assertEquals(node1, root.getChildAt(1)); }