question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tree: Missing code to sort tree

See original GitHub issue

Describe the bug

In PF11 was removed TreeUtils, so because I still need to sort trees I add this manually, but on version PF12 this code stopped working.

    /**
     * Sorts children of a node using a comparator
     *
     * @param node Node instance whose children to be sorted
     * @param comparator Comparator to use in sorting
     */
    public static void sortNode(TreeNode node, Comparator comparator) {
        TreeNodeList children = (TreeNodeList) node.getChildren();

        if (children != null && !children.isEmpty()) {
            Object[] childrenArray = children.toArray();
            Arrays.sort(childrenArray, comparator);
            for (int i = 0; i < childrenArray.length; i++) {
                children.setSibling(i, (TreeNode) childrenArray[i]);
            }

            for (int i = 0; i < children.size(); i++) {
                sortNode(children.get(i), comparator);
            }
        }
    }

Reproducer

Try to use this code on the newest PF12 to sort tree component

    /**
     * Sorts children of a node using a comparator
     *
     * @param node Node instance whose children to be sorted
     * @param comparator Comparator to use in sorting
     */
    public static void sortNode(TreeNode node, Comparator comparator) {
        TreeNodeList children = (TreeNodeList) node.getChildren();

        if (children != null && !children.isEmpty()) {
            Object[] childrenArray = children.toArray();
            Arrays.sort(childrenArray, comparator);
            for (int i = 0; i < childrenArray.length; i++) {
                children.setSibling(i, (TreeNode) childrenArray[i]);
            }

            for (int i = 0; i < children.size(); i++) {
                sortNode(children.get(i), comparator);
            }
        }
    }

Expected behavior

The sort functionality should be provided for this component as in the previous version.

PrimeFaces edition

No response

PrimeFaces version

12.0.0

Theme

all

JSF implementation

All

JSF version

all

Java version

all

Browser(s)

all

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
mellowarecommented, Nov 10, 2022

Ha thanks. It’s the highest form of flattery!

0reactions
jepsarcommented, Nov 10, 2022

@melloware looks like someone plagiarized your solution https://stackoverflow.com/a/74385498/880619

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree Sort - GeeksforGeeks
Tree sort is a sorting algorithm that is based on Binary Search Tree data structure. It first creates a binary search tree from...
Read more >
SORTING THE TREE - LeetCode Discuss
A tree is a connected acyclic graph with no self-loops and multiple edges. When you swap 2 nodes, the node along with its...
Read more >
Sorting in Binary Trees | Baeldung on Computer Science
In this tutorial, we'll explain how to sort a binary tree, and show the time and space complexity calculations of sorting it.
Read more >
Sort an unordered tree by the amount of child-nodes a node has
The code is already calling for sort methods for every Node with sortForest() , starting from the leaves, so that has no place...
Read more >
How to print the nodes of a binary tree in sorted order
Code ; 1. import java.util.Stack; ; 2. /* ; 3. * Java Program to traverse a binary search tree ; 4. * using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found