I made a program where the user inputs as many names as they like in a first name, middle initial (optional), and last name format. I then split the names and assign them to string variables called firstName, middleInitial, and lastName. Since there may be more than one name input, I have added each variable to its own arrayList called fName, mI, and lName. Now I must sort them so that they are in ascending order by last name. Here is where the problem comes in. It's easy enough to sort the last name, but how do I get the mI and fName arrayLists to sort in the same order as the lName arrayList? Here is my code:
static void Main(string[] args)
{
ArrayList fName = new ArrayList();
ArrayList mI = new ArrayList();
ArrayList lName = new ArrayList();
Console.WriteLine("Please enter name: " + "(type quit to exit)");
string inValue = Console.ReadLine(); //prime Readline
while (inValue != "quit")
{
string firstName = "",
middleInitial = "",
lastName = "";
string[] name = inValue.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
firstName = name[0];
fName.Add(firstName);
if (name.Length > 2)
{
middleInitial = name[1];
mI.Add(middleInitial);
}
else
{
middleInitial = string.Empty;
mI.Add(middleInitial);
}
if (name.Length == 2)
{
lastName = name[1];
lName.Add(lastName);
}
else
{
lastName = string.Join(" ", name.Skip(2));
lName.Add(lastName);
}
Console.WriteLine("Please enter name: " + "(type quit to exit)");
inValue = Console.ReadLine();
}
}
ArrayList. UseList<string>instead. - John Saunders