Human Sorting Improved
Sunday, March 30, 2025
Factor has had human-friendly sorting since December 2007. It is unclear if it is related, but Ned Batchelder wrote about “human sorting” around the same time with links to a couple of other blog posts discussing similar topics, so perhaps it was in the zeitgeist of the time.
In any event, Ned recently wrote about “human sorting improved” which deals with the topic of how to sort two strings that are human-equivalent but are different and should probably have an ordering. This was the result of fixing a problem that actually happened in the coverage.py project.
For example, comparing "x1y"
and "x001y"
using the original
algorithm would consider these to be equal given the same human keys: { "x" 1 "y" }
.
You can see this in Factor 0.100:
IN: scratchpad USE: sorting.human
IN: scratchpad "x1y" "x001y" human<=> .
+eq+
Ned suggests that instead – if two strings are equal using the human sorting method – they should be compared for lexicographic ordering as a tie-breaker.
I have made that change in the latest development version of Factor so that the behavior is changed:
IN: scratchpad "x1y" "x001y" human<=> .
+gt+