chiark / gitweb /
compute-scottish-stv: sort countballots output
[appendix-a6.git] / compute-scottish-stv
index 6fde10308b409753d9f877e1df709300d2558fa3..4715808de5057be8fcc5bb30f59ca19c3080df58 100755 (executable)
@@ -155,9 +155,12 @@ sub countballots () {
        next if $c->{NonCont};
        $c->{Total} = 0;
        $c->{Total} += $_->{Weight} foreach @{ $c->{Votes} };
-       prf "candidate %-10s: %10s votes\n", $cand, $c->{Total};
        $c->{History}[$stage-1] = $c->{Total};
     }
+
+    foreach my $c (reverse sort total_history_cmp values %cands) {
+       prf "candidate %-10s: %10s votes\n", $c->{Cand}, $c->{Total};
+    }
 }
 
 sub computequota () {
@@ -244,15 +247,13 @@ sub elect_core ($) {
     $c->{NonCont} = 'Elected';
 }
 
-$stage = 1;
-
-sortballots @allvotes;
-countballots();
-computequota();
+$stage = 0;
 
 for (;;) {
     $stage++;
 
+    sortballots @allvotes if $stage == 1;
+
     my $seats_remain = $seats
        - grep { ($_->{NonCont} // '') eq 'Elected' } values %cands;
     if (continuing() <= $seats_remain) {
@@ -265,6 +266,8 @@ for (;;) {
 
     countballots();
 
+    computequota if $stage == 1;
+
     my $c = select_best_worst
        sub { $_->{Total} >= $quota },
        sub { $_ > $quota },