how to sort?Code:struct student { int id; float score; }s[3]{1,90},{2,80},{2,90}}; int cmp(const void *b,const void *a) { register struct student *p1=(struct student *)a; register struct student *p2=(struct student *)b; return (p1->id)>(p2->id)? 1:( (p1->id)<(p2->id) ? -1:0 ); } qsort(s,10,sizeof(student),cmp);
first: sort by id
second: if id equal. then sort by score
output:
2 90
2 80
1 90