[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Of a bad sort: 1 10 2 3 4 5 6 7 8 9



In article <3mc99l$70h@tools.near.net> barmar@nic.near.net (Barry Margolin) writes:
| ]When there is an embedded numeric string at the same position
| ]in both strings, compare those substrings as if they each occupied
| ]a single character position.
| 
| While this is feasible, I think it's vast overkill and likely to be
| expensive to search every string for numeric substrings.

I think it's actually pretty cheap to implement this, even in
CommonLisp.  You walk the strings left-to-right; if and when you hit
digits in both strings, you note the beginning of the non-zero leading
digit.  You then continue walking forward.  By the time you reach the
end of the digit substrings, they may either have turned out to be the
same, in which case you just continue never looking back at the
digits, or they are different, in which case you just walk backwards
and compare them in reverse lexicographic order.  There is not really
a separate search involved, and most of the loop looks just like the
usual character-wise string comparison function.  The only thing that
you can't do anymore is use word comparison for counted (rather than
zero terminated) strings.

In fact, this might be a nice sorting option for "ls", since people
never seem to remember to put enough leading zeros on their file names
when they are numerical.

On the other hand, I don't think something like this belongs in
a language standard.  It might be part of a standard library.

Now, what about embedded g-format floating point numbers :-)

				Thomas.