Skip to content

Commit

Permalink
Sorting by county and then by contest name.
Browse files Browse the repository at this point in the history
  • Loading branch information
vteague committed Aug 2, 2024
1 parent 24013da commit b34474e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.math.MathContext;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.apache.commons.text.StringEscapeUtils.escapeCsv;
Expand Down Expand Up @@ -133,7 +134,7 @@ public String endpointBody(final Request the_request, final Response the_respons
*/
public String estimateSampleSizes() {
final String prefix = "[estimateSampleSizes]";
final List<estimateData> dataRows = new ArrayList<>();
final List<EstimateData> dataRows = new ArrayList<>();
BigDecimal riskLimit;

// For estimation of sample sizes for each audit, we need to collect the ContestResult
Expand All @@ -155,7 +156,7 @@ public String estimateSampleSizes() {
throw new IllegalStateException(msg);
}

// Iterate over all the contest results, getting relevant data into an estimateData record.
// Iterate over all the contest results, getting relevant data into an EstimateData record.
for (ContestResult cr : countedCRs) {

final ComparisonAudit ca = createAuditOfCorrectType(cr, riskLimit);
Expand All @@ -166,10 +167,13 @@ public String estimateSampleSizes() {
final int contestBallots = CountyContestResultQueries.withContestName(ca.getContestName())
.stream().mapToInt(CountyContestResult::contestBallotCount).sum();

dataRows.add(new estimateData(countyNames, ca, contestBallots, cr.getBallotCount()));
dataRows.add(new EstimateData(countyNames, ca, contestBallots, cr.getBallotCount()));
}

return String.join("\n", dataRows.stream().map(estimateData::toString).toList());
// Sort the data according to the name of the county, and then by contest.
Collections.sort(dataRows);
// dataRows.sort((first, second) -> first.countyName.compareTo(second.countyName()));
return String.join("\n", dataRows.stream().map(EstimateData::toString).toList());
}

/**
Expand All @@ -184,9 +188,9 @@ public String estimateSampleSizes() {
* @param dilutedMargin The margin divided by the total Auditable ballots.
* @param estimatedSamples The estimated samples to audit.
*/
private record estimateData(String countyName, String contestName, String contestType,
private record EstimateData(String countyName, String contestName, String contestType,
int contestBallots, long totalBallots, BigDecimal dilutedMargin,
int estimatedSamples) {
int estimatedSamples) implements Comparable<EstimateData> {

/**
* Construct the record by pulling relevant data out of the ComparisonAudit data structure.
Expand All @@ -197,7 +201,7 @@ private record estimateData(String countyName, String contestName, String contes
* @param contestBallots The number of ballots that actually contained the contest.
* @param totalBallots The total universe of ballots for this contest's audit.
*/
public estimateData(List<String> countyNames, ComparisonAudit ca, int contestBallots, long totalBallots) {
public EstimateData(List<String> countyNames, ComparisonAudit ca, int contestBallots, long totalBallots) {
this(countyNames.size() == 1 ? countyNames.get(0) : "Multiple",
ca.getContestName(),
ca instanceof IRVComparisonAudit ? ContestType.IRV.toString() : ContestType.PLURALITY.toString(),
Expand Down Expand Up @@ -232,5 +236,23 @@ public String toString() {
"" + Math.min(estimatedSamples, totalBallots)
));
}

/**
* For sorting the csv rows. First compare the county names as strings; if they're equal, use the
* contest name as a secondary sort.
* @param anotherRow other row to be compared with.
* @return -1 if this value is (lexicographically by county and contest name) less than the other value,
* 0 if their county and contest names are equal (which is unexpected)
* +1 if this value is greater than the other value.
*/
@Override
public int compareTo(EstimateData anotherRow) {
// If the county names are the same, sort by contest name.
if (countyName.equals(anotherRow.countyName)) {
return contestName.compareTo(anotherRow.contestName());
}
// If the county names are different, sort by county name.
return countyName.compareTo(anotherRow.countyName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import us.freeandfair.corla.query.CountyQueries;

import javax.transaction.Transactional;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

Expand Down Expand Up @@ -180,8 +179,8 @@ void basicEstimatedSampleSizesPluralityAndIRV() {
String csv = response.body();
assertEquals(csv, String.join("\n", List.of(
"County,Contest Name,Contest Type,Ballots Cast,Total ballots,Diluted Margin,Sample Size",
"Adams,pluralityContest,PLURALITY,0,10000,0.12,56",
"Adams,One NEB Assertion Contest,IRV,0,20000,0.32,21"
"Adams,One NEB Assertion Contest,IRV,0,20000,0.32,21",
"Adams,pluralityContest,PLURALITY,0,10000,0.12,56"
)));


Expand Down

0 comments on commit b34474e

Please sign in to comment.