Andrew Briggs
Andrew Briggs

Reputation: 11

How to sort struct by grade, and last name alphabetically

I have this program that needs to be able to sort this input file by letter grade, each one separated by a tab.

Casanova        910017  A
Smith   934422  C
Jones   878766  B
Briggs  123456  D
Anderson        989420  A

But if the grades are the same then it needs needs to sort the last name alphabetically.

Anderson        989420  A
Casanova        910017  A
Jones   878766  B
Smith   934422  C
Briggs  123456  D

I just have no idea how to go about doing this. I have tried several other algorithms that I found but all of them failed with the program only displaying the first name in the list twice.

for(i = 1; i < 14; i++)
{
     j = i - 1;
     while( j >= 0 && strcmp( stu[j+1].last_name, stu[j].last_name ) < 0 )
     {
         temp =  stu[j + 1];
         stu[j+1] = stu[j];
         stu[j] = temp;
         j--;
     }
     printf("\n%s\n",stu[j].last_name);
}

displays

Casanova

Casanova

/

struct student
{
        char last_name[25];
        int stu_id;
        char grade;
}a;
struct student temp, stu[100];

int main(void)
{
     int n, i, cnt = 0;
     char *name, *id, *grd,fgrade, data[2000];
     int iid;
     void *v;
     while(!(fgets(data, 2000, stdin) == NULL))
     {

          name = strtok(data, "\t");
          strcpy(stu[cnt].last_name, name);

          id = strtok(NULL, "\t");
          iid = atoi(id);
          stu[cnt].stu_id = iid;

          grd = strtok(NULL, "\n");
          v = grd;
          fgrade = *((char*) v);
          //printf("%c\n", stu[cnt].last_name[0]);
          stu[cnt].grade = fgrade;

          //printf("\n%s %s %s cnt: %d last_nam = %s\n", name, id, grd, cnt, stu[cnt].last_name);
          ++cnt;
    }
}

I was thinking a qsort but I have never used one before. The output needs to be in this order:

    Anderson        989420  A
    Casanova        910017  A
    Jones   878766  B
    Smith   934422  C
    Briggs  123456  D

Upvotes: 1

Views: 1298

Answers (1)

Ivan Angelov
Ivan Angelov

Reputation: 353

Try this:

typedef struct 
{
        char last_name[25];
        int stu_id;
        char grade;
} student;

student input[5] = {
 {"Casanova",910017,'A'},
 {"Smith",934422,'C'},
 {"Jones",878766,'B'},
 {"Briggs",123456,'D'},
 {"Anderson",989420,'A'}
};

static int cmpstudents(const void *p1, const void *p2)
{
    student *l = (student *)p1;
    student *r = (student *)p2;
    if (l->grade==r->grade)
    {
        return strcmp(l->last_name,r->last_name);
    }
    return (l->grade > r->grade)?(1):(-1);
}

int main ()
{
    int i;
    for (i=0;i<5;i++)
    {
       printf("%s\t%d\t%c\n",input[i].last_name,input[i].stu_id,input[i].grade);
    }
    printf("\n----------------------------\n");
    qsort(input,5,sizeof(student),cmpstudents);
    for (i=0;i<5;i++)
    {
       printf("%s\t%d\t%c\n",input[i].last_name,input[i].stu_id,input[i].grade);
    }

    return 0;
}

The key is to provide your own comparison function to qsort(). The cmpstudents() in case of equal grade will compare name.

Upvotes: 1

Related Questions