sorting help

Posted on 2003-02-26
Medium Priority
Last Modified: 2010-04-15
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.
Question by:solid_hyprid
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8028016
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.

Expert Comment

ID: 8029294
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...

Author Comment

ID: 8032334
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);




Accepted Solution

gj62 earned 300 total points
ID: 8034938
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...
LVL 20

Expert Comment

ID: 10019008
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question