Hello, I am currently making a program that sorts user input of age and name. If a user inputs their name and age, all out of order, how would I go about sorting this from oldest to youngest or youngest to oldest? I have this so far:
Ages[] ages = new Ages[10];
for (int counter = 0; counter < 10; counter++)
{
string tempName;
int tempAge;
Console.WriteLine("Please enter your initials: ");
tempName = Convert.ToString(Console.ReadLine());
Console.WriteLine("Please enter your age: ");
tempAge = Convert.ToInt32(Console.ReadLine());
ages[counter] = new Ages(tempName, tempAge);
}
// Sort array.
Array.Sort(ages);
for (int counter = 0; counter < 10; counter++)
{
Console.WriteLine(ages[counter].ToString());
}
Console.ReadLine();
}
public class Ages
{
public string initials { get; set; }
public int age { get; set; }
public Age(string pInitials, int pAge)
{
initials = pInitials;
age = pAge;
}
public override string ToString()
{
return string.Format("{0} {1}", score, initials);
}
}
}
}