Applied patch from I.Q. to fix problem with sort -n.

This commit is contained in:
Mark Whitley 2001-04-17 17:47:33 +00:00
parent 31868b85be
commit 3828dbed57
2 changed files with 10 additions and 6 deletions

View File

@ -33,7 +33,8 @@ static int compare_ascii(const void *x, const void *y)
static int compare_numeric(const void *x, const void *y) static int compare_numeric(const void *x, const void *y)
{ {
return atoi(*(char **)x) - atoi(*(char **)y); int z = atoi(*(char **)x) - atoi(*(char **)y);
return z ? z : strcmp(*(char **)x, *(char **)y);
} }
int sort_main(int argc, char **argv) int sort_main(int argc, char **argv)
@ -70,6 +71,7 @@ int sort_main(int argc, char **argv)
while ((line = get_line_from_file(fp)) != NULL) { while ((line = get_line_from_file(fp)) != NULL) {
lines = xrealloc(lines, sizeof(char *) * (nlines + 1)); lines = xrealloc(lines, sizeof(char *) * (nlines + 1));
line[strlen(line) - 1] = '\0';
lines[nlines++] = line; lines[nlines++] = line;
} }
} }
@ -81,10 +83,10 @@ int sort_main(int argc, char **argv)
#ifdef BB_FEATURE_SORT_REVERSE #ifdef BB_FEATURE_SORT_REVERSE
if (reverse) if (reverse)
for (i = nlines - 1; 0 <= i; i--) for (i = nlines - 1; 0 <= i; i--)
fputs(lines[i], stdout); puts(lines[i]);
else else
#endif #endif
for (i = 0; i < nlines; i++) for (i = 0; i < nlines; i++)
fputs(lines[i], stdout); puts(lines[i]);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

8
sort.c
View File

@ -33,7 +33,8 @@ static int compare_ascii(const void *x, const void *y)
static int compare_numeric(const void *x, const void *y) static int compare_numeric(const void *x, const void *y)
{ {
return atoi(*(char **)x) - atoi(*(char **)y); int z = atoi(*(char **)x) - atoi(*(char **)y);
return z ? z : strcmp(*(char **)x, *(char **)y);
} }
int sort_main(int argc, char **argv) int sort_main(int argc, char **argv)
@ -70,6 +71,7 @@ int sort_main(int argc, char **argv)
while ((line = get_line_from_file(fp)) != NULL) { while ((line = get_line_from_file(fp)) != NULL) {
lines = xrealloc(lines, sizeof(char *) * (nlines + 1)); lines = xrealloc(lines, sizeof(char *) * (nlines + 1));
line[strlen(line) - 1] = '\0';
lines[nlines++] = line; lines[nlines++] = line;
} }
} }
@ -81,10 +83,10 @@ int sort_main(int argc, char **argv)
#ifdef BB_FEATURE_SORT_REVERSE #ifdef BB_FEATURE_SORT_REVERSE
if (reverse) if (reverse)
for (i = nlines - 1; 0 <= i; i--) for (i = nlines - 1; 0 <= i; i--)
fputs(lines[i], stdout); puts(lines[i]);
else else
#endif #endif
for (i = 0; i < nlines; i++) for (i = 0; i < nlines; i++)
fputs(lines[i], stdout); puts(lines[i]);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }