Using Linq it becomes very easy to produce an ordered set of grades/students with the highest-score:
using System.Collections.Generic;
using System.Linq;
private void ReportGrades()
{
String[] A = { "John", "David", "Clare", "Robert", "Michael", "Peter", "Steve", "Daniel", "Alex", "Bart" };
int[] B = { 56, 78, 95, 23, 95, 87, 61, 77, 45, 33 };
Dictionary<int,List<string>> grades = new Dictionary<int,List<string>>();
for (int i = 0; i < A.Length; i++)
{
if (! grades.Keys.Contains(B[i])) grades[B[i]] = new List<string>();
grades[B[i]].Add(A[i]);
}
var gradesDescending = grades.OrderByDescending(grade => grade.Key).ToList();
foreach (var gradeResult in gradesDescending)
{
Console.Write("Grade: {0}\t", gradeResult.Key.ToString());
foreach (var name in gradeResult.Value)
{
Console.Write("{0} ", name);
}
Console.WriteLine();
}
}
Grade: 95 Clare Michael
Grade: 87 Peter
Grade: 78 David
Grade: 77 Daniel
Grade: 61 Steve
Grade: 56 John
Grade: 45 Alex
Grade: 33 Bart
Grade: 23 Robert </string>