Skip to content

Commit 3e647d4

Browse files
committed
Simplify and clarify t.sort_descendants()
1 parent b02364f commit 3e647d4

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

ete4/core/tree.pyx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,16 +1218,9 @@ cdef class Tree(object):
12181218
ops.ladderize(self, topological, reverse)
12191219

12201220
def sort_descendants(self, prop='name'):
1221-
"""Sort branches by node names.
1222-
1223-
After the tree is sorted, if duplicated names are present,
1224-
extra criteria should be added to sort nodes.
1225-
"""
1226-
node2content = self.get_cached_content(prop, container_type=list)
1227-
1228-
for n in self.traverse():
1229-
if not n.is_leaf:
1230-
n.children.sort(key=lambda x: str(sorted(node2content[x])))
1221+
"""Sort branches by leaf node values (names or any other given prop)."""
1222+
leaf_values = self.get_cached_content(prop, container_type=list)
1223+
ops.sort(self, key=lambda node: tuple(sorted(leaf_values[node])))
12311224

12321225
def get_cached_content(self, prop=None, container_type=set,
12331226
leaves_only=True):

0 commit comments

Comments
 (0)