/** * Alphabetically sorts a range of menu items. * * @param parent Parent whose children are to be sorted alphabetically. * @param startIndex Index of first child to be sorted. * @param endIndex Index of last child to be sorted. */ public static void sortMenu(BaseComponent parent, int startIndex, int endIndex) { List<BaseComponent> items = parent.getChildren(); int bottom = startIndex + 1; for (int i = startIndex; i < endIndex;) { BaseComponent item1 = items.get(i++); BaseComponent item2 = items.get(i); if (item1 instanceof BaseMenuComponent && item2 instanceof BaseMenuComponent && ((BaseMenuComponent) item1) .getLabel().compareToIgnoreCase(((BaseMenuComponent) item2).getLabel()) > 0) { parent.swapChildren(i - 1, i); if (i > bottom) { i -= 2; } } } }
/** * Moves item1 to follow item2. * * @param item1 = A tree node. * @param item2 = A tree node. */ private void swap(Treenode item1, Treenode item2) { if (item1 != null && item2 != null) { item1.getParent().swapChildren(item1.getIndex(), item2.getIndex()); doChanged(true); } }
/** * Alphabetically sorts children under the specified parent. * * @param parent Parent whose child nodes (Treenode) are to be sorted. * @param recurse If true, sorting is recursed through all children. */ public static void sort(BaseComponent parent, boolean recurse) { if (parent == null || parent.getChildren().size() < 2) { return; } int i = 1; int size = parent.getChildren().size(); while (i < size) { Treenode item1 = (Treenode) parent.getChildren().get(i - 1); Treenode item2 = (Treenode) parent.getChildren().get(i); if (compare(item1, item2) > 0) { parent.swapChildren(i - 1, i); i = i == 1 ? 2 : i - 1; } else { i++; } } if (recurse) { for (BaseComponent child : parent.getChildren()) { sort(child, recurse); } } }
parent.swapChildren(startIndex, endIndex);