Skip to content

Instantly share code, notes, and snippets.

@iwinux
Created October 21, 2011 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwinux/1304584 to your computer and use it in GitHub Desktop.
Save iwinux/1304584 to your computer and use it in GitHub Desktop.
字符串字典排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 6
#define N 5
/* reverse strcmp to get descending sort */
int cmp(char *s1, char *s2) {
return -strcmp(s1, s2);
}
int main(int argc, char *argv[]) {
int i;
char strings[M][N];
for (i = 0; i < M; i++) {
scanf("%s", &strings[i]);
}
/* reference: http://www.codecogs.com/reference/c/stdlib.h/qsort.php */
qsort(strings, M, N, cmp);
printf("The result is:\n");
for (i = 0; i < M; i++) {
printf("%s\n", strings[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment