From 3e647d42e44c56fb42be688de42b045af843c5a1 Mon Sep 17 00:00:00 2001 From: Jordi Date: Tue, 28 Nov 2023 14:13:07 +0100 Subject: [PATCH] Simplify and clarify t.sort_descendants() --- ete4/core/tree.pyx | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/ete4/core/tree.pyx b/ete4/core/tree.pyx index 48c565e0..7e25115e 100644 --- a/ete4/core/tree.pyx +++ b/ete4/core/tree.pyx @@ -1218,16 +1218,9 @@ cdef class Tree(object): ops.ladderize(self, topological, reverse) def sort_descendants(self, prop='name'): - """Sort branches by node names. - - After the tree is sorted, if duplicated names are present, - extra criteria should be added to sort nodes. - """ - node2content = self.get_cached_content(prop, container_type=list) - - for n in self.traverse(): - if not n.is_leaf: - n.children.sort(key=lambda x: str(sorted(node2content[x]))) + """Sort branches by leaf node values (names or any other given prop).""" + leaf_values = self.get_cached_content(prop, container_type=list) + ops.sort(self, key=lambda node: tuple(sorted(leaf_values[node]))) def get_cached_content(self, prop=None, container_type=set, leaves_only=True):