From dc683b0762d8e2792a4afaee4af1b5ef58a45390 Mon Sep 17 00:00:00 2001 From: Raz Date: Thu, 31 Oct 2024 18:32:37 +0100 Subject: [PATCH] fix LB sorting --- tournaments/models/round.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tournaments/models/round.py b/tournaments/models/round.py index feca1d0..f1b47f0 100644 --- a/tournaments/models/round.py +++ b/tournaments/models/round.py @@ -67,11 +67,22 @@ class Round(models.Model): matches.sort(key=lambda m: m.index) # Recursively fetch matches from child rounds - for child_round in self.children.all(): + # Sort children by depth in ascending order (deeper rounds last) + sorted_children = sorted(self.children.all(), key=lambda child: child.get_depth()) + + for child_round in sorted_children: matches.extend(child_round.get_matches_recursive(hide_empty_matches)) return matches + def get_depth(self): + depth = 0 + current_round = self + while current_round.parent: + depth += 1 + current_round = current_round.parent + return depth + def root_round(self): if self.parent is None: return self