Skip to content

Instantly share code, notes, and snippets.

@BenGoldberg1
Last active April 28, 2016 16:08
Show Gist options
  • Save BenGoldberg1/4a91e9b7c106736c0ffaa2c350bbd75c to your computer and use it in GitHub Desktop.
Save BenGoldberg1/4a91e9b7c106736c0ffaa2c350bbd75c to your computer and use it in GitHub Desktop.
int goldberg_compare( const void *ap, const void bp ) {
const char **aa = ap, **bb = bp;
const char *a = *aa, *b = *bb;
do {
int achars, bchars;
long aval, bval;
int shorter, cmp;
while( isspace(*a) ) ++a;
while( isspace(*b) ) ++b;
achars = strcspn( a, "0123456789" );
bchars = strcspn( b, "0123456789" );
shorter = min( achars, bchars );
cmp = memcmp( a, b, shorter );
if( cmp ) return cmp;
if( achars != bchars ) return bchars - achars;
a += achars;
b += bchars;
if( !*a || !*b ) return (!!*b) - (!!*a);
aval = strtol( a, &a, 10 );
bval = strtol( b, &b, 10 );
if( aval != bval ) return bval - aval;
} while(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment