Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
At the start of class, I like to call roll. I like to go through my list of students in alphabetical order. Where possible, I like to call students by their first names. Of course, if two students have the same first name, I have to also give the last name so they know who I’m calling. Write a program to help me out. Given a class roll, it is going to tell how I should call the names.

Input consists of up to 
 names, one per line, terminated by the end of file. Each line contains a first and a last name for a particular person. First and last names use 
 letters (a–z), always starting with an uppercase letters first followed by only lowercase letters. No two people will have exactly the same first and last names.

Print the list of names, one per line, sorted by last name. If two or more people have the same last name, order these people by first name. Where the first name is unambiguous, just list the first name. If two people have the same first name, also list their last names to resolve the ambiguity.

Sample Input 1	
Will Smith
Agent Smith
Peter Pan
Micky Mouse
Minnie Mouse
Peter Gunn

Sample Output 1
Peter Gunn
Peter Pan

What I have tried:

namespace ClassRoll
    public class Program
        static void Main(string[] args)
            List<string> names = new List<string>();

            // Read input names until end of file
            string input;
            while ((input = Console.ReadLine()) != "")

            // Sort the names using a custom comparer
            names.Sort(new NameComparer());

            // Print the sorted names
            foreach (string name in names)
    public class NameComparer : IComparer<string>
        public int Compare(string x, string y)
            // Split the names into first and last name
            string[] nameX = x.Split(' ');
            string[] nameY = y.Split(' ');

            string firstNameX = nameX[0];
            string firstNameY = nameY[0];

            string lastNameX = nameX[1];
            string lastNameY = nameY[1];

            // Compare last names
            int FirstNameComparison = firstNameX.CompareTo(firstNameY);
            if (FirstNameComparison != 0) //first names are the same
                return FirstNameComparison;

            //string firstNameX = nameX[0];
            //string firstNameY = nameY[0];

            // Compare first names if last names are the same
            return firstNameX.CompareTo(firstNameY);
Updated 17-May-23 1:22am
Graeme_Grant 17-May-23 6:52am    
If you were a teacher you would not be asking here. This sounds more like homework.

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

I'd start by thinking about what you need to do: instead of a list of strings, I'd create a Student class, and create a collection of those. Give it firstname and surname properties, and get it to separate each student name input (which I'd suggest should come from a file rather than use input - your assignment does suggest this). Then it's simple: Linq methods include OrderBy and ThenBy which will do what you need.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900