1
votes

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();
    }
}
3
Is this homework? There's no practical reason to keep separate lists otherwise. - Yuck
And there's no reason to use ArrayList. Use List<string> instead. - John Saunders
I'll research that. The book mentions it but that's about it. - Programming Newbie

3 Answers

2
votes

Use a single list instead of 3. To do this you will need a data structure that encapsulates the first, middle, and last names into a single class.

public class Name
{
  public string First { get; set; }
  public string Middle { get; set; }
  public string Last { get; set; }
}

And instead of using an ArrayList use a List<T> instead. Next use LINQ to do the sorting for you.

var list = new List<Name>();
list.Add(new Name { First = "Brian",   Middle = "D", Last = "Gideon" });
list.Add(new Name { First = "Bart",    Middle = "",  Last = "Simpson" });
list.Add(new Name { First = "Captain", Middle = "",  Last = "America" });
var ordered = list.OrderBy(x => x.Last).ThenBy(x => x.First).ThenBy(x => x.Middle);
foreach (Name item in ordered)
{
  Console.WriteLine(item.Last + ", " + item.First + " " + item.Middle);
}
1
votes

Don't use 3 lists. Use 1 list, whose elements contain first, middle and last name and implement IComparable so the sort is done on the last name first (and then by first and possibly middle name).

Alternatively, elements don't need to implement IComparable if you supply appropriate IComparer to List.Sort.

You can easily split this list into 3 lists if you want, after the sort.

1
votes

Create a class UserName contains 3 fields: fName, mName, lName. public class UserName { public string fName, mName, lName; } Instead of storing data into 3 arrays, store data into 1 ArrayList.

class Program
    {
        static void Main(string[] args)
        {
              List<Username> usernames = new ArrayList<Username>();
        }
    }

As user inputs first name, middle name, last name, create new Username object and stores it into ArrayList.

usernames.add(new Username {
fName = firstName,
mName = middleName,
lName = lastName}
);

To sort this ArrayList: -Create IComparable object to sort (by fName or lName).

public class sortByFNameHelper : IComparer
{
   int IComparer.Compare(object a, object b)
   {
      UserName u1=(UserName)a;
      UserName u2=(UserName)b;
      if (u1.fName > u2.fName)
         return 1;
      if (u1.fName < u2.fName)
         return -1;
      else
         return 0;
   }
}
  • Call sort on ArrayList:

    IComparer compareByFName = new sortByFNameHelper(); usernames.sort(compareByFName);

HTH.