#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
typedef struct in {
int x;
int y;
}In;
typedef struct char_ {
char ch[100];
}Char_;
//对字符串排序函数
int ptr_7(const void *a, const void *b) {
return strcmp((*(Char_ *)a).ch, (*(Char_ *)b).ch);
}
//结构体一级排序函数
int ptr_5(const void *a, const void *b) {
return (*(In *)a).x>(*(In *)b).x ? 1 : -1;
}
//结构体二级排序
int ptr_6(const void *a, const void *b) {
In *c = (In *)a;
In *d = (In *)b;
if (c->x != d->x)return c->x - d->x;
else
return c->y - d->y;
}
//整数排序比较函数
int ptr_1(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
//double型排序比较函数
int ptr_2(const void *a, const void *b) {
return *(double *)a>*(double *)b ? 1 : -1;
}
//char型排序比较函数
int ptr_3(const void *a, const void *b) {
return *(char *)a - *(char *)b;
}
//对二维数组排序
int ptr_4(const void *a, const void *b) {
return ((int *)a)[0] - ((int *)b)[0];
}
int main() {
int i, j, k, l;
int a[5] = { 4,2,1,7,3 };
double b[5] = { 3.21,4.35,5.34,86.3,12.4 };
char c[5] = { 'g','t','a','v','p' };
int d[3][2] = { { 5,4 },{ 1,6 },{ 9,0 } };
qsort(a, 5, sizeof(a[0]), ptr_1);
qsort(b, 5, sizeof(b[0]), ptr_2);
qsort(c, 5, sizeof(c[0]), ptr_3);
qsort(d, 3, sizeof(int) * 2, ptr_4);
for (i = 0; i<5; i++) {
printf("%d ", a[i]);
}
printf("
");
for (i = 0; i<5; i++) {
printf("%lf", b[i]);
}
printf("
");
for (i = 0; i<5; i++) {
printf("%c", c[i]);
}
for (i = 0; i<3; i++) {
for (j = 0; j<2; j++) {
printf("%d", d[i][j]);
}
printf("
");
}
In data[10];
printf("输入结构体值");
for (i = 0; i<10; i++) {
scanf("%d%d", &data[i].x, &data[i].y);
}
qsort(data, 10, sizeof(data[0]), ptr_5);
for (i = 0; i<10; i++) {
printf("x=%d,y=%d
", data[i].x, data[i].y);
}
printf("----------------------------");
qsort(data, 10, sizeof(data[0]), ptr_6);
for (i = 0; i<10; i++) {
printf("x=%d,y=%d
", data[i].x, data[i].y);
}
Char_ ch[5];
for (i = 0; i<5; i++) {
scanf("%s", ch[i].ch);
}
qsort(ch, 5, sizeof(ch[0]), ptr_7);
for (i = 0; i<5; i++) {
printf("%s
", ch[i].ch);
}
getch();
return 0;
}