fix human sort -- wrap all elements so that integers dont get compared against strings directly
parent
16b288aac8
commit
daf1594390
|
@ -1,4 +1,14 @@
|
||||||
USING: sorting.human tools.test sorting.slots ;
|
USING: sorting.human tools.test sorting.slots sorting ;
|
||||||
IN: sorting.human.tests
|
IN: sorting.human.tests
|
||||||
|
|
||||||
[ { "x1y" "x2" "x10y" } ] [ { "x1y" "x10y" "x2" } { human<=> } sort-by ] unit-test
|
[ { "x1y" "x2" "x10y" } ]
|
||||||
|
[ { "x1y" "x10y" "x2" } { human<=> } sort-by ] unit-test
|
||||||
|
|
||||||
|
[ { "4dup" "nip" } ]
|
||||||
|
[ { "4dup" "nip" } [ human<=> ] sort ] unit-test
|
||||||
|
|
||||||
|
[ { "4dup" "nip" } ]
|
||||||
|
[ { "nip" "4dup" } [ human<=> ] sort ] unit-test
|
||||||
|
|
||||||
|
[ { "4dup" "4nip" "5drop" "nip" "nip2" "nipd" } ]
|
||||||
|
[ { "nip" "4dup" "4nip" "5drop" "nip2" "nipd" } [ human<=> ] sort ] unit-test
|
||||||
|
|
|
@ -6,4 +6,15 @@ IN: sorting.human
|
||||||
: find-numbers ( string -- seq )
|
: find-numbers ( string -- seq )
|
||||||
[EBNF Result = ([0-9]+ => [[ string>number ]] | (!([0-9]) .)+)* EBNF] ;
|
[EBNF Result = ([0-9]+ => [[ string>number ]] | (!([0-9]) .)+)* EBNF] ;
|
||||||
|
|
||||||
<< "human" [ find-numbers ] define-sorting >>
|
! For comparing integers or sequences
|
||||||
|
TUPLE: hybrid obj ;
|
||||||
|
|
||||||
|
M: hybrid <=>
|
||||||
|
[ obj>> ] bi@
|
||||||
|
2dup [ integer? ] bi@ xor [
|
||||||
|
drop integer? [ +lt+ ] [ +gt+ ] if
|
||||||
|
] [
|
||||||
|
<=>
|
||||||
|
] if ;
|
||||||
|
|
||||||
|
<< "human" [ find-numbers [ hybrid boa ] map ] define-sorting >>
|
||||||
|
|
Loading…
Reference in New Issue