Gregg:
> REBOL's sort *is* stable, AFAIK, per Holger. If you provide your own
> comparison function then you need to return -1, 0, or 1 (rather than
> true/false) and it should still be stable. I haven't done any tests to
prove
> it, so someone please correct me if I'm wrong
Thanks -- that looks like the case.
Rebol's sort looks stable with those three return values, and unstable
without them. I learn something new every day!! The Core User Guide could do
with an update -- it only mentions true/false returns from a sort comparison
function.
The slightly inelegant code below demonstrates the difference in the two
sort-return methods:
data: [
"1 first of the ones"
"1 last of the ones"
"3 top three"
"3 middle three"
"3 bottom three"
"2 start of the twos"
"2 middle of the twos"
"2 last of the twos"
]
print "unstable sort on 1st field"
sort/compare sorted-data: copy data func [a b] [
return (first parse a " ") < (first parse b " ")
]
print mold sorted-data
print "stable sort on first field"
sort/compare sorted-data: copy data func [a b] [
if (first parse a " ") > (first parse b " ") [return +1]
if (first parse a " ") < (first parse b " ") [return -1]
return 0
]
print mold sorted-data
Sunanda.
--
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the
subject, without the quotes.