/**
   * Sort the categories from the root.
   *
   * @param root The root element.
   */
  private static void sortCategories(TreeElement root) {
    List<CategoryElement> categories = new ArrayList<CategoryElement>(root.getChildCount());

    for (int i = 0; i < root.getChildCount(); i++) {
      categories.add((CategoryElement) root.getChild(i));
    }

    root.clear();

    Collections.sort(
        categories,
        new Comparator<CategoryElement>() {
          @Override
          public int compare(CategoryElement o1, CategoryElement o2) {
            return o1.getElementName().compareToIgnoreCase(o2.getElementName());
          }
        });

    root.addAll(categories);

    for (CategoryElement element : categories) {
      if (!element.isLeaf()) {
        sortCategories(element);
      }
    }
  }
  /**
   * Sort the specified tree model.
   *
   * @param model The tree model to sort.
   */
  public void sort(JThequeTreeModel model) {
    TreeElement root = model.getRoot();

    root.clear();

    Collection<Category> categories = categoriesService.getCategories();
    Map<Category, CategoryElement> elements =
        new HashMap<Category, CategoryElement>(categories.size());

    addCategories(root, categories, elements);

    sortCategories(root);

    addMovies(root, elements);

    model.setRootElement(root);
  }