Мне нужна небольшая помощь в сортировке разбитого массива в наивысшем до наименьшего, сохраняя имена рядом с оценками. Я немного не уверен, как это сделать, потому что массив разбит.Как отсортировать массив строк и ints?
Это код, который у меня есть до сих пор.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace proj09LEA
{
class Program
{
static void Main(string[] args)
{
// declare and array of integers
string[] name = new string[5];
int[] score = new int[5];
Console.WriteLine("\nSaturday Coder's Bowling Team");
Console.WriteLine("Enter in a name and score for each person on the team.");
Console.WriteLine("For example, Mary 143. Just hit Enter when you are done.\n");
// fill an array with user input
for (int i = 0; i < score.Length; i++)
{
Console.WriteLine("Enter in a name and score: ");
string line = Console.ReadLine();
name[i] = line.Substring(0, line.IndexOf(' '));
score[i] = int.Parse(line.Substring(line.IndexOf(' ') + 1));
}
Console.WriteLine("------------ Input Complete ------------\n");
Console.WriteLine("Here are the scores for this game, from highest to lowest:\n");
for (int i = 0; i < score.Length; i++)
{
if (score[i] >= 300)
{
Console.WriteLine("{0}'s score was {1}*.", name[i], score[i]);
}
else
{
Console.WriteLine("{0}'s score was {1}.", name[i], score[i]);
}
}
dynamic swap;
for (int i = 0; i < score.Length - 1; i++)
{
for (int j = i + 1; j < score.Length; j++)
{
swap = score[i];
score[i] = score[j];
score[j] = swap;
swap = name[i];
name[i] = name[j];
name[j] = swap;
}
}
AverageScore(score);
Console.WriteLine("Press Enter to continue. . .");
Console.ReadLine();
}
static void AverageScore(int[] score)
{
int sum = score.Sum();
int average = sum/score.Length;
Console.WriteLine("The average score for this game was {0:d}.\n", average);
}
}
}
Я бы настоятельно рекомендовал использовать * единственную * коллекцию обоих оценок и имен, объединенных в класс 'Player' или что-то подобное. См. Http://codeblog.jonskeet.uk/2014/06/03/anti-pattern-parallel-collections/ –