sorting.slots: make sort-keys-by and sort-values-by support hashtables.

modern-harvey2
John Benediktsson 2017-07-05 16:45:23 -07:00
parent 6649e4a426
commit 46d4b33349
2 changed files with 10 additions and 3 deletions

View File

@ -93,3 +93,8 @@ TUPLE: tuple2 d ;
{ { 3 { 2 4 } } { 1 { 2 0 0 0 } } { 0 { 1 } } { 1 { 1 } } }
{ length-test<=> <=> } sort-values-by
] unit-test
{ { { "apples" 1 } { "bananas" 2 } { "cherries" 3 } } } [
H{ { "apples" 1 } { "bananas" 2 } { "cherries" 3 } }
{ { sequences:length <=> } } sort-keys-by
] unit-test

View File

@ -1,6 +1,6 @@
! Copyright (C) 2009 Slava Pestov, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays fry kernel math.order sequences sorting ;
USING: arrays assocs fry kernel math.order sequences sorting ;
IN: sorting.slots
: execute-comparator ( obj1 obj2 word -- <=>/f )
@ -23,6 +23,8 @@ IN: sorting.slots
: sort-by ( seq sort-specs -- seq' ) [ ] sort-by-with ;
: sort-keys-by ( seq sort-seq -- seq' ) [ first ] sort-by-with ;
: sort-keys-by ( alist sort-seq -- seq' )
[ >alist ] dip [ first ] sort-by-with ;
: sort-values-by ( seq sort-seq -- seq' ) [ second ] sort-by-with ;
: sort-values-by ( seq sort-seq -- seq' )
[ >alist ] dip [ second ] sort-by-with ;