# PTA 7-37 模拟EXCEL排序（自定义排序函数）

• 自定义排序函数

Excel可以对一组纪录按任意指定列排序。现请编写程序实现类似功能。

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

000001 Zoe 60
000007 James 85
000010 Amy 90

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

#define MAXN 100010

int N, C;

struct Stu
{
int id;
char name[10];
} stus[MAXN];

bool cmp1(Stu a, Stu b)
{
return a.id < b.id;
}

bool cmp2(Stu a, Stu b)
{
if (strcmp(a.name, b.name) == 0)
return a.id < b.id;
else
return strcmp(a.name, b.name) < 0;
}

bool cmp3(Stu a, Stu b)
{
return a.id < b.id;
else
}

int main()
{
freopen("data.txt", "r", stdin);
scanf("%d%d", &N, &C);
getchar();
for (int i = 0; i < N; i++)
{
}
if (C == 1)
{
sort(stus, stus + N, cmp1);
}
else if (C == 2)
{
sort(stus, stus + N, cmp2);
}
else if (C == 3)
{
sort(stus, stus + N, cmp3);
}
for (int i = 0; i < N; i++)
{
if(i != N - 1)
printf("%06d %s %d\n", stus[i].id, stus[i].name, stus[i].grade);
else
printf("%06d %s %d", stus[i].id, stus[i].name, stus[i].grade);
}
return 0;
}
12-16 1万+

12-02 225
05-13 3740
05-03 1880
10-23 1074
04-27 1137
02-08 2317
10-25 1656
03-13 372
05-31 9083
12-29 2万+
04-10 877
08-30 4127
03-29 9609
12-17 4789
10-10 2675