1

抽出した配列リストをソート済みリストに拡張する方法を見つけようとしていますか?以下に、各候補に基づいて結果を水平方向に表示するコードを示しますが、これをSortedListに組み込んで、境内で並べ替えることができる方法を確認しています。

並べ替えリストでいくつかのバリエーションを試しましたが、最初の列(のみ)を垂直方向に印刷することが唯一の成功でした。

X 012

X 075

X 050

X 040

string[] voting = { "X012033001140", "C075100026080", "A050070060100", "Q040088050090" };
//int length = voting.Length;
int starter = 0;

int total_a = 0;
int total_b = 0;
int total_c = 0;
int total_d = 0;

int precinct_x = 0;

Console.WriteLine("VOTING RESULTS");
Console.WriteLine("--------------------");

Console.WriteLine("Precinct\tCandidate A \tCandidate B\tCandidate C\tCandidate D\tTotal Precinct Votes");

while (starter < voting.Length)
{
    string precinct = voting[starter].Substring(0, 1);
    string cand_a = voting[starter].Substring(1, 3);
    string cand_b = voting[starter].Substring(4, 3);
    string cand_c = voting[starter].Substring(7, 3);
    string cand_d = voting[starter].Substring(10, 3);
    int counter = 0;

    //Converting the vote counts from strings to ints in order to calculate
    int a = Convert.ToInt32(cand_a);
    int b = Convert.ToInt32(cand_b);
    int c = Convert.ToInt32(cand_c);
    int d = Convert.ToInt32(cand_d);

    precinct_x = a + b + c + d;
    counter++;

    //Console.WriteLine(precinct);
    Console.WriteLine(precinct + "\t\t" + cand_a + "\t\t" + cand_b + "\t\t" + cand_c + "\t\t" + cand_d + "\t\t" + precinct_x + "\t");

    double precinct_total = Convert.ToDouble(precinct_x);

    //This is required in order to continuously loop up to the maximum of the voting Length
    starter = starter + 1;

    //Calculations for Candidate A
    total_a = total_a + a;

    total_b = total_b + b;
    total_c = total_c + c;
    total_d = total_d + d;

}
Console.WriteLine("\nTOTAL RESULTS");
Console.WriteLine("--------------------------------------------------------------------------------");
Console.WriteLine("\t\t" + total_a + "\t\t" + total_b + "\t\t" + total_c + "\t\t" + total_d);

Console.ReadLine();
4

1 に答える 1

0

まず、並べ替えるデータを保持できるクラスを作成する必要があります

public class VotingResult
{
    public VotingResult(int numberOfCandidates)
    {
        Candidates = new int[numberOfCandidates];
    }

    public string Precinct { get; set; }
    public int[] Candidates { get; private set; }
    public int PrecinctTotal { get { return Candidates.Sum(); } }
}

次にリストに記入します

const int NumberOfCandidates = 4;
var votingResults = new List<VotingResult>();
foreach (string s in voting) {
    votingResult = new VotingResult(NumberOfCandidates);
    votingResult.Precinct = s.Substring(0, 1);
    for (int cand = 0; cand < NumberOfCandidates; cand++) {
        votingResult.Candidates[cand] = Convert.ToInt32(s.Substring(3 * cand + 1, 3));
    }
    votingResults.Add(votingResult);
}

これで、リストを並べ替えることができます

var sortedResults = votingResults.OrderBy(v => v.Precinct);

// Or, depending on what you want to sort

var sortedResults = votingResults.OrderByDescending(v => v.PrecinctTotal);

今、あなたは印刷することができます

var totals = new int[NumberOfCandidates];
foreach (VotingResult v in sortedResults) {
    Console.WriteL(v.Precinct + "\t\t");
    for (int cand = 0; cand < NumberOfCandidates; cand++) {
        Console.Write(v.Candidates[cand]  + "\t\t");
        total[cand] += v.Candidates[cand];
    }
    Console.WriteLine(v.PrecinctTotal);
}
Console.WriteLine("\nTOTAL RESULTS\t\t");
for (int cand = 0; cand < NumberOfCandidates; cand++) {
     Console.Write(totals[cand]  + "\t\t");
}
Console.WriteLine();

並べ替えと合計にLINQを使用しているため、コードの先頭にあるこの行を忘れないでください。

using System.Linq;

原因のを使用することもできますSortedList。これは、並べ替えられた結果に数回アクセスする必要がある場合、またはリストにエントリを追加する必要があり、リストを最後だけでなく常に並べ替えたい場合に便利です。

var sortedList = new SortedList<string, VotingResult>();

...
sortedList.Add(votingResult.Precinct, votingResult);
...

または、行の合計を降順で並べ替える場合

var sortedList = new SortedList<int, VotingResult>();

...
sortedList.Add(-votingResult.PrecinctTotal, votingResult);
...

ソート部分を削除でき、ループする必要があることを除いて、残りは同じままですsortedList.Values


これを行うには多くの方法がありますが、いずれの場合も、すべてのデータを一緒に並べ替えることができるため、リストエントリのクラスを作成すると便利です。

于 2013-03-17T00:57:56.273 に答える