|
|
@ -100,6 +100,15 @@ class CommentTree: |
|
|
|
compare equal on the sorting method will be the same as the order that they were |
|
|
|
originally in when passed to this function. |
|
|
|
""" |
|
|
|
# sort the tree's comments first, this will be important when sorting by newest |
|
|
|
# reply |
|
|
|
for comment in tree: |
|
|
|
if not comment.has_visible_descendant: |
|
|
|
# no need to bother sorting replies if none will be visible |
|
|
|
continue |
|
|
|
|
|
|
|
comment.replies = CommentTree._sort_tree(comment.replies, sort) |
|
|
|
|
|
|
|
if sort == CommentTreeSortOption.NEWEST: |
|
|
|
tree = sorted(tree, key=lambda c: c.created_time, reverse=True) |
|
|
|
elif sort == CommentTreeSortOption.POSTED: |
|
|
@ -108,13 +117,15 @@ class CommentTree: |
|
|
|
tree = sorted(tree, key=lambda c: c.num_votes, reverse=True) |
|
|
|
elif sort == CommentTreeSortOption.RELEVANCE: |
|
|
|
tree = sorted(tree, key=lambda c: c.relevance_sorting_value, reverse=True) |
|
|
|
|
|
|
|
for comment in tree: |
|
|
|
if not comment.has_visible_descendant: |
|
|
|
# no need to bother sorting replies if none will be visible |
|
|
|
continue |
|
|
|
|
|
|
|
comment.replies = CommentTree._sort_tree(comment.replies, sort) |
|
|
|
elif sort == CommentTreeSortOption.NEWEST_REPLY: |
|
|
|
# sort by the creation time of the most recent reply in the entire tree, or |
|
|
|
# the comment itself if it has no children. the comment's first reply is |
|
|
|
# the newest reply since it has been sorted above |
|
|
|
tree = sorted( |
|
|
|
tree, |
|
|
|
key=lambda c: (c.replies[0] if c.replies else c).created_time, |
|
|
|
reverse=True, |
|
|
|
) |
|
|
|
|
|
|
|
return tree |
|
|
|
|
|
|
|