From 84aa8c64742454196652e22ed9c4798cd3535432 Mon Sep 17 00:00:00 2001 From: Felix Sargent Date: Fri, 24 Jan 2025 22:27:08 +0000 Subject: [PATCH] Sort by proportional percentage --- approval_polls/views.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/approval_polls/views.py b/approval_polls/views.py index c292098..88bb49e 100644 --- a/approval_polls/views.py +++ b/approval_polls/views.py @@ -309,18 +309,24 @@ def get_context_data(self, **kwargs): proportional_votes[choice_id] += weight total_proportional_votes += weight - proportional_results = [ - { - "choice_text": choice.choice_text, - "proportional_votes": proportional_votes[choice.id], - "proportional_percentage": ( - proportional_votes[choice.id] / total_proportional_votes * 100 - if total_proportional_votes > 0 - else 0 - ), - } - for choice in poll.choice_set.all() - ] + proportional_results = sorted( + [ + { + "choice_text": choice.choice_text, + "proportional_votes": proportional_votes[choice.id], + "proportional_percentage": ( + proportional_votes[choice.id] / total_proportional_votes * 100 + if total_proportional_votes > 0 + else 0 + ), + } + for choice in poll.choice_set.all() + ], + key=lambda x: x[ + "proportional_percentage" + ], # Sort by proportional_percentage + reverse=True, # Highest percentage first + ) # Add data to context context.update(