[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Numeric sorting in GNU ls
From: |
Markus Kuhn |
Subject: |
Numeric sorting in GNU ls |
Date: |
Thu, 21 Feb 2002 20:22:41 +0000 |
Is there way to tell ls to sort filename "2" before file "10"?
In order words, can the sorting order be made to follow
/*
* Compare two strings just like strcmp, but preserve decimal integer
* sorting order, i.e. "2" < "10". Strings are sorted as if sequences
* of digits were prefixed by a length indicator. Does not ignore
* leading zeroes.
*/
int strcmpn(const char *s1, const char *s2)
{
int digits, predigits = 0;
const char *ss1, *ss2;
while (1) {
if (*s1 == 0 && *s2 == 0)
return 0;
digits = isdigit(*s1) && isdigit(*s2);
if (digits && !predigits) {
ss1 = s1;
ss2 = s2;
while (isdigit(*ss1) && isdigit(*ss2))
ss1++, ss2++;
if (!isdigit(*ss1) && isdigit(*ss2))
return -1;
if (isdigit(*ss1) && !isdigit(*ss2))
return 1;
}
if ((unsigned char)*s1 < (unsigned char)*s2)
return -1;
if ((unsigned char)*s1 > (unsigned char)*s2)
return 1;
predigits = digits;
s1++, s2++;
}
}
It would be nice to be able to configure that sorting behaviour in a
command line option, as it would lead to more sensible ordering in very
many situations.
Markus
--
Markus G. Kuhn, Computer Laboratory, University of Cambridge, UK
Email: mkuhn at acm.org, WWW: <http://www.cl.cam.ac.uk/~mgk25/>
- Numeric sorting in GNU ls,
Markus Kuhn <=