结构体排序的几种情况
1.一个结构体中有三个元素,按照其中一个元素进行降序排列:
先定义一个结构体:
struct node
{
int s;
int t;
int w;
}a[1005];
然后写排序代码:
int cmp(node a, node b)
{
return a.s > ;
}
在main函数里面用sort进行排序:
sort(a, a+ n, cmp);
2:排序要求:按分数从高到低输出上线考生的考号与分数,其间用1空格分隔。若有多名考生分数相同,则按他们考号的升序输出。
定义一个结构体:
struct Student
{
char str[30]; //考生准考证号
char num[20]; //存题号
int sum; // 考生的最后得分
}student[1010];
然后写排序代码:
int cmp(Student a, Student b)
{
if(a.sum == b.sum)
{
return a.str < b.str;
}
return a.sum >b.sum;
}
sort(student, student+n, cmp);