]> gitweb.factorcode.org Git - factor.git/commitdiff
sorting.slots: make sort-keys-by and sort-values-by support hashtables.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 5 Jul 2017 23:45:23 +0000 (16:45 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 5 Jul 2017 23:46:06 +0000 (16:46 -0700)
basis/sorting/slots/slots-tests.factor
basis/sorting/slots/slots.factor

index 2b56d5dfe8e59fac6b78fee7a68becb52b8ecec2..72b6481c45fd4f8f24e040e75400d88f971e6693 100644 (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
index 2ef95afa1b19bab7b0b89dd87e25b6e2f8337391..05333935d15ddfc04d24f9ac0c3e9f9ab3c01f2b 100644 (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 ;