Make human-sort behave like sort

db4
Doug Coleman 2009-01-07 15:58:33 -06:00
parent 638f1f4ceb
commit 6d6e691083
1 changed files with 13 additions and 3 deletions

View File

@ -1,10 +1,20 @@
! Copyright (C) 2008 Doug Coleman, Slava Pestov. ! Copyright (C) 2008 Doug Coleman, Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: peg.ebnf math.parser kernel assocs sorting ; USING: peg.ebnf math.parser kernel assocs sorting fry
math.order sequences ascii splitting.monotonic ;
IN: sorting.human 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-sort ( seq -- seq' ) : human-<=> ( obj1 obj2 -- <=> ) [ find-numbers ] bi@ <=> ;
[ dup find-numbers ] { } map>assoc sort-values keys ;
: human-compare ( obj1 obj2 quot -- <=> ) bi@ human-<=> ;
: human-sort ( seq -- seq' ) [ human-<=> ] sort ;
: human-sort-keys ( seq -- sortedseq )
[ [ first ] human-compare ] sort ;
: human-sort-values ( seq -- sortedseq )
[ [ second ] human-compare ] sort ;