• C

sorting help

Hi, Can someone give me some advice for this program?  Especially on getting the user input and sorting it accordingly!

First your program will ask the user how manys names will be inputed. Number of names will not exceed 100. Then ask the user for the names. Names will be in the format of "Last,First,age" with nospaces and there will always be
three fields. Assume the user will always use this format. The names string will be at most 40 characters. After the user has inputed the names, ask how are they to be sorted. They are sorted by either last, first, or age. Then print the names out in sorted order. For people who have the same last name, sort by first name. For people with the same age, sort by last name and if that is also the same sort by first name. For people with the same first name, sort by last name. No two people will have the same first and last name.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I'm thinking that you would want to read in the values using scanf() or gets() or equivalent.  Then copy the data into a doubly-linked list in sorted order:

typedef struct name_list
  char *first_name;
  char *last_name;
  int age;

  name_list *prev;
  name_list *next;
struct name_list *first;

You would sit there reading in names with scanf() or gets() or something along those lines.  Then you'd loop over the input searching for commas.  At each comma you'd copy the data into a temporary variable.  Once you have parsed out the first name, last name, and age you would loop through the linked list looking for the proper location for this entry (using strcmp() or stricmp() to test for alphabetical ordering).  This way you sort as you read in the values and don't have to worry about having a gather data phase and a sort phase, you just have a smart gather data phase.

This assumes that you're familiar with malloc() and strcpy() to make room for the data in a string / create new entries in the list and to copy that data to the proper location.

I appologize for being rusty on C struct syntax as I've been a C++ junkie instead of a C junkie for a couple years now.

Good luck with your assignment.
First, we can't give you code because as a homework assignment it is against the board policies.

Second, egore's approach will only work with a single sort order as written - you could add pointers to keep track of the other sort orders, but you really have to want to do it that way.

Third, egore's approach for getting the data from the user is the way to go.

If you don't care about memory (with a max of 100 structs, it is no big deal), you can just declare an array of 100 structs.

You can use qsort with different compare functions for name vs age - I will leave that as a task for you to try first, then you can post your efforts and we will help you if you need it...
solid_hypridAuthor Commented:
Heres what i have so far....Can someone help me on the sorting...and tell me if i'm going into the right direction?  Thanks~!

#include <stdio.h>
#include "names_func.h"

void getnames(char names[][LEN], int num){

int i;
printf("Enter name %d:", i+1);
scanf("%s", names[i]);


void sort_names(char names[][LEN], int num, int type){
  int i=0,j=0,x,iagetemp=0;
   char first[MAX][FIRST+LAST],last[MAX][LAST+FIRST];
  char age[MAX][AGE+LAST+FIRST];
  char agetemp[FIRST+LAST+AGE];

// last name


  strncpy(last[x], &names[x][0], i);       // last is the array of last names
  last[x][i] = '\0';

  strncpy(first[x][LEN], &names[x][i+1], j); //   first is the array of first names
  first[x][j-i] = '\0';
  strcat(first[x],last[x]);   // i think this connects first and last name

  strcpy(agetemp, &names[x][j+2]);
    iagetemp = atoi(agetemp);



OK - is this homework?  If so, you probably can't use qsort() - if not, take a look at it - it will make the problem trivial.

If you've got to do your own sort, and this is homework, look at this link (yes, it does have complete source code, but it also has good explanations on sorting...).


They are internal sorts - just what you need for this project...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by gj62.


EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.