将数组数据添加到sortedlist中
本文关键字:sortedlist 添加 数组 数据 | 更新日期: 2023-09-27 18:04:10
我想找到一种方法,我可以扩展我提取的数组列表到排序列表?下面是我的代码,它根据每个候选人水平显示结果,但是我看到如何将其合并到SortedList中,以便按选区排序?
我尝试了一些排序列表的变化,但是我唯一成功的是垂直打印出第一列。
012年X 075年X 050年X 040年Xstring[] 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();
首先,您需要创建一个类来保存要排序的数据
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
。
有许多方法可以做到这一点,但无论如何,为列表项创建一个类是有利的,因为它允许您将所有数据排序在一起。