[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 <3m9lsi$s1@Yost.com> yost@Yost.com (Dave Yost) writes:
]    1
]    10
]    2
]
]Most people would say the latter is not sorted.  

That's why "order" is relative to a particular comparison function.
Lexicographically, it's ordered, but it's not ordered numerically.

]						  Yet we continue
]to write software that sorts strings this way.  Why do we do it?
]Because the available string comparison functions embody a
]straightforward, simple implementation--which happens to be wrong
]when the sorting of the result is done solely for the convenience
]of a user.

(sort list-of-strings #'< :key parse-integer)

will handle the above simple case.

]I propose that the following functions be added to Common Lisp
]so that sorting appropriate for humans can be accomplished
]straightforwardly and efficiently:
]
]   human-string<
]   human-string>
]   human-string<=
]   human-string>=
]   human-string-lessp
]   human-string-greaterp
]   human-string-not-greaterp
]   human-string-not-lessp
]
]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.  If all the
strings are expected to have the numeric portion at the same position, then
that position is probably known by the application.  Therefore, it would be
more appropriate to use a comparison function designed specifically for
that application, which knows how to parse the strings and extract the
numeric portion.
-- 
Barry Margolin
BBN Planet Corporation, Cambridge, MA
barmar@bbnplanet.com