#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu{
char name[20];
int age;
};
int sort_by_age(const void* e1, const void* e2){
return ((struct stu*)e1)->age - ((struct stu*)e2)->age;
}
int sort_by_name(const void* e1, const void* e2){
return strcmp(((struct stu*)e1)->name, ((struct stu*)e2)->name);
//strcmp比较的是对应字母的ASCII码值
}
void test(){
struct stu s[] = { { "zhangsan", 30 }, { "lisi", 34 }, { "wangwu", 20 } };
int sz = sizeof(s) / sizeof(s[0]);
//qsort(s, sz, sizeof(s[0]), sort_by_age);//按年龄比较
qsort(s, sz, sizeof(s[0]), sort_by_name);//按名字比较
}
int main(){
test();
return 0;
}