1 /* COLLATE COMPARE, COMPARES DIGITS NUMERICALLY AND OTHERS IN ASCII */
4 * Copyright 2001, 2015, Harlan Stenn. Used by NTP with permission.
6 * Author: Harlan Stenn <harlan@pfcs.com>
8 * Copying and distribution of this file, with or without modification,
9 * are permitted in any medium without royalty provided the copyright
10 * notice and this notice are preserved. This file is offered as-is,
11 * without any warranty.
15 * Expected collate order for numeric "pieces" is:
18 * 000 - 999 followed by
25 * Older versions of isdigit() require the argument be isascii()
29 # define MyIsDigit(x) \
30 (isascii ((unsigned char) (x)) && isdigit ((unsigned char) (x)))
32 # define MyIsDigit(x) isdigit ((unsigned char) (x))
41 int hilo = 0; /* comparison value */
48 hilo = (*s1 < *s2) ? -1 : (*s1 > *s2) ? 1 : 0;
55 hilo = (*s1 < *s2) ? -1 : (*s1 > *s2) ? 1 : 0;
60 hilo = 1; /* s2 is first */
62 hilo = -1; /* s1 is first */
69 hilo = -1; /* s1 must come first */
74 hilo = 1; /* s2 must come first */
77 hilo = (*s1 < *s2) ? -1 : (*s1 > *s2) ? 1 : 0;
87 return ((*s1 < *s2) ? -1 : (*s1 > *s2) ? 1 : 0);
94 static int qcmp( const void *fi1,
97 return colcomp(*(char**)fi1, *(char**)fi2);
100 int main( int argc, char *argv[], char *environ[]) {
103 size_t size = sizeof(char *);
105 "999", "0", "10", "1", "01", "100", "010", "99", "00", "001", "099", "9"
111 /* Sort use-provided list */
114 nmemb = sizeof ca / size;
116 printf("argc is <%d>, nmemb = <%d>\n", argc, nmemb);
120 for (i = 0; i < nmemb; ++i) {
121 printf("%s\n", *cp++);
124 qsort((void *)base, nmemb, size, qcmp);
128 for (i = 0; i < nmemb; ++i) {
129 printf("%s\n", *cp++);