I'm fairly new to the C Programming language but am currently taking a course on it. One of the projects is to write a phonebook/contacts list. All of my phonebook works except for listing contacts in alphabetical order by first name while also keeping the last name and phone number associated with the correct first name. I am unsure as to how to accomplish this.

Is there any way to take multiple strings and consolidate them to a single one where I could then tell the code to list that single string in alphabetical order? (This should work regardless of the number of contacts added to the phonebook. Assume all contacts have unique names and numbers).

Here is a portion of my code showing only what has to be while omitting everything except what needs to be changed. If more code is needed I can post what is necessary. Any help is appreciated. I am using DevC++ however this is only C Language.

Code:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <malloc.h>

//Structure for phonebook list
typedef struct phonebook 
{
        char firstname[50];
        char lastname[50];
        char phonenumber[50];
} list;

list *contacts;

//Function prototypes & global variables
void addcontact(struct phonebook *contacts, int count);
void deletecontact(struct phonebook *contacts, int count);
void listcontacts(struct phonebook *contacts, int count);
void randomcontact(struct phonebook *contacts, int i);
void deleteall(struct phonebook *contacts, int count);
void showsearch(struct phonebook *contacts, int count);
void sortcontacts(struct phonebook *contacts, int count);
int searchcontacts(struct phonebook *contacts, int count, char *name);
struct phonebook *contacts;
int choice;
int count = 0; 
int tally = 0;

//Phonebook menu
int main()
{
    contacts = (struct phonebook *)malloc(sizeof(struct phonebook));
    srand(time(NULL));
    
    do
    {
        printf("\n--PHONEBOOK MENU--\n");
        printf("1. Add Contact\n");
        printf("2. Delete Contact\n");
        printf("3. List Contacts\n");
        printf("4. Alphabetical List\n");
        printf("5. Search Contacts\n");
        printf("6. Random Contact\n");
        printf("7. Delete All Contacts\n");
        printf("8. Exit\n");
        printf("To make a selection, enter a number 1-7:\n");
        scanf("%d", &choice);
        
        switch (choice)
        {
            case 1:        //Adds Contact
                addcontact(contacts, count);
                count++;
                break;
                
            case 2:        //Deletes single contact
                deletecontact(contacts, count);
                count--;
                printf("\n--CONTACT SUCCESSFULLY DELETED--\n");
                break;
                
            case 3:        //Lists contacts
                listcontacts(contacts, count);
                printf("\n");
                break;
            
            case 4:        //Lists contacts alphabetically
                sortcontacts(contacts,count);
                printf("\n");
                break;
                
            case 5:        //Search contacts
                showsearch(contacts, count);
                printf("\n");
                break;
            
            case 6:        //Lists random contact
                tally = rand() % count;
                randomcontact(contacts, tally);
                printf("\n");
                break;
                
            case 7:        //Deletes all contacts
                count = 0;
                printf("\n--ALL CONTACTS SUCCESSSFULLY DELETED--\n");
                printf("\n");
                break;
                
            case 8:        //Exits phonebook
                printf("\n--PHONEBOOK EXITED--\n");
                return 0;
        }//End switch
    }//End do
    
    while (1);
    
    //Return used memory
          free(contacts);
          contacts = NULL;
           
          return 0;
}//End main

//Function to add contacts
void addcontact(struct phonebook *contacts, int count)
{
    printf("\n--ADD NEW CONTACT--");
    printf("\nEnter contacts name and number (First Last Number):\n");
    scanf("%s %s %s", contacts[count].firstname, contacts[count].lastname, contacts[count].phonenumber);
    printf("\n--CONTACT SUCCESSFULLY ADDED--\n");
}//End addcontact


//Function to list contacts alphabetically by first name
void sortcontacts(struct phonebook *contacts, int count)
{
    int i, j;
    char tempname[50];
    for (i = 0; i < count; j++)
    {
    if (strcmp(contacts[i].firstname, contacts[j].firstname) > 0)
        {
        strcpy(tempname, contacts[i].firstname);
        strcpy(contacts[i].firstname, contacts[j].firstname);
        strcpy(contacts[j].firstname, tempname);
        printf("\n%s %s %s\n", contacts[j].firstname, contacts[i].lastname, contacts[i].phonenumber);
        }//End if     
    }//End for
}//End sortcontacts