構造体のデータをソートしたいです。
Posted: 2013年6月27日(木) 15:41
お世話になっております。C言語について質問です。こちらのプログラムでindexの項目に構造体をソートしたいのですがソートできません。間違いなどございましたらご指摘いただけるとありがたいです。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct
{
char country_code[4];
char name[64];
char num[4];
int index;
char date[8];
}STUDENT;
int CMP (const void *p, const void *q)
{
return ((STUDENT *) p)->num, ((STUDENT *) q)->num;
}
int main (void)
{
STUDENT student[] = {
{"111","rarara","12",90,"08/08"},
{"121","nanana","10",80,"09/20"},
{"122","hahaha","15",70,"01/10"},
{"120","bababa","50",100,"12/26"}
};
int i;
qsort (student, sizeof (student)/sizeof (student[0]), sizeof (student[0]), CMP);
for(i=0;i< sizeof (student)/sizeof (student[0]);i++)
printf("%s %s\n",student[i].country_code,student[i].num);
return 0;
}