[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
More sort suggestions
From: |
Charlie Gordon |
Subject: |
More sort suggestions |
Date: |
Wed, 5 May 2004 20:59:31 +0200 |
Another idea for sort I've been toying with in my own utilities : natural
order.
It is basically lexico-graphical order, locale specific or not, with one
departure when encountering digits.
strings that compare equal upto a substring of digits [0-9]+ are then
compared as if -n and back to lexicographical if numbers compare equal. A
simple implementation follows:
int natcmp(const char *s1, const char *s2)
{
while (*s1) {
if (isdigit(*s1) && isdigit(*s2)) {
unsigned long n1 = strtoul(s1, &s1, 10);
unsigned long n2 = strtoul(s2, &s2, 10);
if (n1 == n2)
continue;
return (n1 < n2) ? -1 : 1;
}
if (*s1 != *s2)
break;
s1++;
s2++;
}
return (*s1 < *s2) ? -1 : (*s1 != *s2);
}
This allows sorting chapter files in a more human friendly order
Take a shot at `infocmp xterm` and look at the order for function key
escapes (kf*=)
Is this implemented anywhere un textutils or core-utils ?
Does it belong here ?
How about an option in /bin/ls ?
Charlie Gordon.