A program that can sort a list of names and telephone numbers alphabetically. Persons are sorted alphabetically by their first names. Persons with the same first name are sorted by their last names.

Following are the guidelines for program,program must include these things.

Input: names and telephone numbers,
Output: list of names and telephone numbers ordered alphabetically.
Define a structure named person that contains a person's first name, last name, and telephone number.
Define a function that compares two structures of type person according to the following prototype:
int compare(person * a, person * b);
This function should return a negative value if person a comes before b, and a positive value if b comes before a. (You may use the function stricmp(name1,name2) to compare two strings).
Define a function that swaps the contents of two structures of type person:
void swap(person * x, person * y);

I think you should do your homework on you own, at least put up your own code and then ask about suggestion.

Here are some code just for reference.

struct Person{
 char firstname[32];
 char lastname[32];
 char telnum[32];

return: -1 if a<b
        0 if a==b
        1 if a>b
int compare(Person *a, Person *b){
    if( strcmp(a->firstname,b->firstname) ){
        return strcmp(a->firstname,b->firstname);
    if( strcmp(a->lastname,b->lastname) ){
        return strcmp(a->lastname,b->lastname);
    return 0;

void swap(Person *x, Person *y){
    Person t;
    t = *x;
    *x = *y;
    *y = t;

Edited 2 Years Ago by WenLiang: submit by mistake

Not the best example, but a good first post! :-)
This article has been dead for over six months. Start a new discussion instead.