new strcomp() function.

M

Malcolm

Ever noticed when looking at a list of computer-sorted items that you get
entries like

aardvark
aardvark1
aardvark10
aardvark2
....

This is because the sorting program is calling strcmp() internally.

Here's a replacement that solves this problem.

int compstr(const char *s1, const char *s2)
{
long n1, n2;
char *end1, *end2;

while(*s1 && *s2)
{
if(isdigit(*s1))
{
if(isdigit(*s2))
{
n1 = strtol(s1, &end1, 10);
n2 = strtol(s2, &end2, 10);
if(n1 < n2)
return -1;
if(n1 > n2)
return 1;

s1 = end1;
s2 = end2;
}
else
return *s1 - *s2;
}
else if(*s1 == *s2)
{
s1++;
s2++;
}
else
return *s1 - *s2;
}

if(*s2)
return -1;
if(*s1)
return 1;

return 0;
}

A few issues remain. For instance, this function only works in ANSI or other
lexigraphically-ordered character set.

Also

aardvark1

and

aardvark001

will compare as equal.

Also it doesn't understnad decimal points. I don't know if this is an
advantage or not. In books you will commonly find figures labelled

fig1.1 ... fig1.9 fig1.10

on the other hand, 1.10 is lower than 1.9 in the decimal system.
 
A

Arthur J. O'Dwyer

Ever noticed when looking at a list of computer-sorted items that you get
entries like

aardvark
aardvark1
aardvark10
aardvark2
...

This is because the sorting program is calling strcmp() internally.

Here's a replacement that solves this problem.

<snip code>

You might be interested in this site:
http://sourcefrog.net/projects/natsort/
which seems to have [links to] a few C implementations of
"natural string comparison" functions, plus some Perl ones.
(Frankly, I was surprised this wasn't already built into
"standard" Perl somewhere -- but that's off-topic here.)

A few issues remain. For instance, this function only works in ANSI
or other lexigraphically-ordered character set.

Meaning that it sorts 'Anteater' before 'aardvark', and so on,
right?
Also
aardvark1
and
aardvark001
will compare as equal.

You could always solve this problem by replacing all your

return 0;
with
return strcmp(original_first_string, original_second_string);

I don't have any quibbles with the code itself -- looks pretty
good to me!

HTH,
-Arthur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top