Closed Dako390 closed 2 years ago
Hi, I think you can rather easily obtain this by walking the resulting tree and sorting the children arrays in place.
@mihneadb Can you share an example. I'm also working on a tree view but the the directories and files aren't sorted.
@arunkumar413 I have a solution for you (although your structure seems sorted?).
After receiving the data I just used the following method:
/**
* Method to sort the tree: directories first, then files alphabetically
*
* @param {IProjectNode} node root to begin sorting with
*/
private sortTree(node: IProjectNode) {
if (node.children) {
node.children.forEach(child => {
if (child.children) {
this.sortTree(child);
}
});
node.children.sort(function (x, y) {
if (x.type === y.type) {
if (x.name.toUpperCase() < y.name.toUpperCase()) {
return -1;
} else {
return 1;
}
}
return x.type > y.type ? 1 : -1;
});
}
}
I am using a flat mat tree for my case and IProjectNode is an interface I am using for the nodes. My backend uses the default method "directoryTree()".
Is there an easy way to use
directoryTree()
resulting in a structured tree with directories first and then files?Current result
Currently it returns the tree sorted alphabetically and it does not prioritizes wether it is a directory or file.
Desired result
First give me all directories sorted alphabetically, then all files sorted alphabetically. For reference see the Windows explorer or Visual Studio Code explorer.
Thanks in advance!