Tree: Missing code to sort tree
See original GitHub issueDescribe 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:
- Created 10 months ago
- Comments:13 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Ha thanks. It’s the highest form of flattery!
@melloware looks like someone plagiarized your solution https://stackoverflow.com/a/74385498/880619