Last active
May 27, 2019 13:10
-
-
Save Loliver1224/2a5517974928a759e89d6f639499f7cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
typedef struct{ | |
int id; | |
char name[20]; | |
} dict; | |
int intcmp(const void *a, const void *b){ | |
return ((dict *)a)->id - ((dict *)b)->id; | |
} | |
int intrcmp(const void *a, const void *b){ | |
return ((dict *)b)->id - ((dict *)a)->id; | |
} | |
int stringcmp(const void *a, const void *b){ | |
return strcmp(((dict *)a)->name, ((dict *)b)->name); | |
} | |
int stringrcmp(const void *a, const void *b){ | |
return strcmp(((dict *)b)->name, ((dict *)a)->name); | |
} | |
void output(dict *data){ | |
int i; | |
for(i=0; i<3; i++) | |
printf("%3d %s\n", data[i].id, data[i].name); | |
} | |
int main(void){ | |
dict data[3] = {{1, "Alice"}, {3, "Bob"}, {2, "Chris"}}; | |
puts("ID降順"); | |
qsort(data, 3, sizeof(dict), intrcmp); | |
output(data); | |
puts("ID昇順"); | |
qsort(data, 3, sizeof(dict), intcmp); | |
output(data); | |
puts("名前降順"); | |
qsort(data, 3, sizeof(dict), stringrcmp); | |
output(data); | |
puts("名前昇順"); | |
qsort(data, 3, sizeof(dict), stringcmp); | |
output(data); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment