5 Replies - 686 Views - Last Post: 28 July 2015 - 03:32 PM Rate Topic: -----

#1 andrew343   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-July 15

order a list by last and first name and display back to the user

Posted 27 July 2015 - 07:57 PM

I am trying to build a contact managers program using a list to store and display the data. I need to view a report that displays a summary of contacts available and then have a menu to allow the user to interact with the program. I have a method to create a list with data and a contact object. I also have a method to sort by first name and last name but I need to display the list sorted back to the user. I am not sure as to how to do this.

Any guidance would be appreciated

static void Main(string[] args)
        {
            

            //Declare the list

            List<Contact> contactList = new List<Contact>();

            Console.WriteLine("Contact List");
            
            //Main Driver
            char menuItem;
            Console.WriteLine("Welcome to the player system...\n");
            menuItem = GetMenuItem();
            while (menuItem != 'Q')
            {

                ProcessMenuItem(menuItem, contactList);
                menuItem = GetMenuItem();

            }
            Console.WriteLine("\nThank you, goodbye");
            Console.ReadLine();
        }
        //Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller
        static char GetMenuItem()
        {
            char menuItem;
            DisplayMenu();
            menuItem = char.ToUpper(IOConsole.GetChar("\nPlease pick an item: "));
            while (menuItem != 'C'
                && menuItem != 'R' && menuItem != 'Q' && menuItem != 'U' && menuItem != 'D' && menuItem != 'S' && menuItem != 'L' && menuItem != 'F' && menuItem != 'P' && menuItem != 'T')
            {
                Console.WriteLine("\nError - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(IOConsole.GetChar("Enter option or M for menu:"));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
           Console.WriteLine("C-> Create Contacts");
           Console.WriteLine("R-> Remove Contacts");
           Console.WriteLine("U-> Update Contacts");
           Console.WriteLine("D -> Load data from file");
           Console.WriteLine("S-> Save data to file");
           Console.WriteLine("L-> View sorted by last name");
           Console.WriteLine("F-> View sorted by first name");
           Console.WriteLine("P-> View by partial name search");
           Console.WriteLine("T-> View by contact type");
           Console.WriteLine("Q-> Quit");
        }

        //Routes to the appropriate process routine based on the user menu choice
        static void ProcessMenuItem(Char menuItem, List<Contact> contactList)
        {
            switch (menuItem)
            {
                case 'C':
                    createContact();
                    break;
                case 'R':
                    removeContact(contactList);
                    break;
                case 'U':
                    updateContact(contactList);
                    break;
                case 'D':
                    LoadToFile();
                    break;
                case 'S':
                    saveToFile();
                    break;
                    
                case 'L':
                    sortByLastName(contactList);
                    break;
                case 'F':
                    sortByFirstName(contactList);
                       break;
                case 'P':
                       DisplayList(contactList);
                       break;
                case 'T':
                       sortByContactType();
                       break;
                case 'Q':
                       
                       break;

            }                   
        }
public static void sortByLastName(List<Contact> contactList )
        {
            contactList.OrderBy(c => c.GetLastName);
            
            
        }
         public static void sortByFirstName(List<Contact> contactList)
        {
            contactList.OrderBy(c => c.GetFirstName);
        }



Is This A Good Question/Topic? 0
  • +

Replies To: order a list by last and first name and display back to the user

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2688
  • View blog
  • Posts: 10,767
  • Joined: 03-December 12

Re: order a list by last and first name and display back to the user

Posted 27 July 2015 - 08:11 PM

A few things.

this:

Quote

	                && menuItem != 'R' && menuItem != 'Q' && menuItem != 'U' && menuItem != 'D' && menuItem != 'S' && menuItem != 'L' && menuItem != 'F' && menuItem != 'P' && menuItem != 'T')


Would be better suited in an array or list. Then use .Contains to see if it is a valid option. That gets rid of the extreme if statement arguments.

As far as displaying the list... call the function that displays the list after you perform the sort.

This post has been edited by astonecipher: 27 July 2015 - 08:12 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrew343   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-July 15

Re: order a list by last and first name and display back to the user

Posted 27 July 2015 - 09:43 PM

I am trying to build a contact managers program in a console application using a list to store and display the data. I need to view a report that displays a summary of contacts available and then have a menu to allow the user to interact with the program. I have a method to create a list with data and a contact object. I need to use my DisplayList method to sort by a partial name search, which should match against matches in both first and last names, ascending by last name but I am not sure as to how to do this.

any guidance would be appreciated

//Declare the list

            List<Contact> contactList = new List<Contact>();
                       
            //Main Driver
            char menuItem;
             Console.WriteLine("Contact List\n");
            menuItem = GetMenuItem();
            while (menuItem != 'Q')
            {

                ProcessMenuItem(menuItem, contactList);
                menuItem = GetMenuItem();

            }
            Console.WriteLine("\nThank you, goodbye");
            Console.ReadLine();
        }
        //Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller
        static char GetMenuItem()
        {
            char menuItem;
            DisplayMenu();
            menuItem = char.ToUpper(IOConsole.GetChar("\nPlease pick an item: "));
            while (menuItem != 'C'
                && menuItem != 'R' && menuItem != 'Q' && menuItem != 'U' && menuItem != 'D' && menuItem != 'S' && menuItem != 'L' && menuItem != 'F' && menuItem != 'P' && menuItem != 'T')
            {
                Console.WriteLine("\nError - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(IOConsole.GetChar("Enter option or M for menu:"));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
           Console.WriteLine("C-> Create Contacts");
           Console.WriteLine("R-> Remove Contacts");
           Console.WriteLine("U-> Update Contacts");
           Console.WriteLine("D -> Load data from file");
           Console.WriteLine("S-> Save data to file");
           Console.WriteLine("L-> View sorted by last name");
           Console.WriteLine("F-> View sorted by first name");
           Console.WriteLine("P-> View by partial name search");
           Console.WriteLine("T-> View by contact type");
           Console.WriteLine("Q-> Quit");
        }

        //Routes to the appropriate process routine based on the user menu choice
        static void ProcessMenuItem(Char menuItem, List<Contact> contactList)
        {
            switch (menuItem)
            {
                case 'C':
                    createContact();
                    break;
                case 'R':
                    removeContact(contactList);
                    break;
                case 'U':
                    updateContact(contactList);
                    break;
                case 'D':
                    LoadToFile();
                    break;
                case 'S':
                    saveToFile();
                    break;
                    
                case 'L':
                    sortByLastName(contactList);
                    break;
                case 'F':
                    sortByFirstName(contactList);
                       break;
                case 'P':
                       DisplayList(contactList);
                       break;
                case 'T':
                       sortByContactType();
                       break;
                case 'Q':
                       
                       break;

            }                   
        }
 public static void DisplayList(List<Contact> contactList)
        {
            
        }


Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5315
  • View blog
  • Posts: 14,185
  • Joined: 18-April 07

Re: order a list by last and first name and display back to the user

Posted 27 July 2015 - 10:21 PM

Linq to the rescue...

var results = contactList.FindAll(n => n.name.Contains("<your partial match text here>")).OrderBy(n => n.name);

foreach (Contact a in results) {
   Console.WriteLine("{0}", a.name);
}



Of course you can modify this to use contains on first or last name of your contact and same with orderby. Here I just use "name" but you can do your firstname or lastname fields. Here "n" would be your contact object that you test against and sort by.

:)

This post has been edited by Martyr2: 27 July 2015 - 10:23 PM

Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: order a list by last and first name and display back to the user

Posted 28 July 2015 - 03:21 AM

Threads on same topic merged
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: order a list by last and first name and display back to the user

Posted 28 July 2015 - 03:32 PM

andrew343: If you haven't grasped it by now let me just tell you directly - Stop creating new threads every day for this same thing. They keep getting closed or merged and will continue to be closed and merged to this.

People are giving you good advice right here. So what's the problem?
Nobody here is going to do your work for you or write code for you to copy/paste into your project/homework.

If you don't understand the advice then say that, IN THIS THREAD, asking for clarification.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1