A common way to implement it on systems with ASCII-compatible character sets (including UTF-8) is to loop until the characters differ, or they are nuls. Then, the return value is
((unsigned char)(character_in_s1) - (unsigned char)(character_in_s2)).
Aw crap, here you go:
// For qsort() on arrays of strings (arrays of pointers to char or unsigned char)
int compare(const void **p1, const void **p2)
{
const unsigned char *const s1 = *(const unsigned char **)p1;
const unsigned char *const s2 = *(const unsigned char **)p2;
size_t i = 0;
while (s1[i] == s2[i] && s1[i] != '\0') {
i++;
}
return (s1[i]) - (s2[i]);
}
or, for a case-insensitive comparison that considers all whitespace equal and considers decimal numbers by value, using
tolower() and
isspace() and
isdigit() from
<ctype.h> (use
<locale.h> and
setlocale(LC_ALL, "") to make it use your locale at run time):
// For qsort() on arrays of strings (arrays of pointers to char or unsigned char)
int humancompare(const void **p1, const void **p2)
{
const unsigned char *s1 = *(const unsigned char **)p1;
const unsigned char *s2 = *(const unsigned char **)p2;
while (1) {
unsigned char c1 = *(s1++);
unsigned char c2 = *(s2++);
if (isdigit(c1) && isdigit(c2)) {
const unsigned char *end1 = s1;
const unsigned char *end2 = s2;
// Skip to end of decimal number in s1
while (isdigit(*end1))
end1++;
// Skip to end of decimal number in s2
while (isdigit(*end2))
end2++;
// Digit offset
size_t i = 0;
// Maximum number of digits to examine
const size_t n1 = (size_t)(end1 - s1);
const size_t n2 = (size_t)(end2 - s2);
// is the smaller of the two
const size_t n = (n1 < n2) ? n1 : n2;
// Compare decimal numbers from right to left
while (i < n) {
const int d = end1[-i] - end2[-i];
if (d)
return d; // Digits differ
else
i++;
}
if (i < n1) {
// First string was shorter
return -1;
} else
if (i < n2) {
// Second string was shorter
return +1;
} else {
// Decimal numbers match, continue after them
s1 = end1;
s2 = end2;
continue;
}
}
if (isspace(c1)) {
// Consider all consecutive whitespace equivalent to a single space
while (isspace(*s1))
s1++;
c1 = ' ';
} else {
// Convert to lower case
c1 = tolower(c1);
}
if (isspace(c2)) {
// Consider all consecutive whitespace equivalent to a single space
while (isspace(*s2))
s2++;
c2 = ' ';
} else {
// Convert to lower case
c2 = tolower(c2);
}
// We have a result if the characters differ or both strings end.
if (c1 != c2 || c1 == '\0')
return c1 - c2;
}
}
For human text, you'd need to use wide character strings and
iswspace(),
iswdigit(), and
towctrans() (with desc=
wctrans("tolower") and desc=wctrans("toupper), since not all letters in all languages have both lower- and uppercase versions).
The conversion between "normal" multibyte strings and wide strings is done using for example
mbstowcs() and
wcstombs().
This way, if you localize your program properly using
setlocale(LC_ALL, "") (or for character set and types only using
setlocale(LC_CTYPE, "")), you can sort in human-preferred order in whatever locale the user has set.