Reputation: 113
I am currently trying to figure out a way to re-arrange entries within an existing map based, on a function apllied to its values, such as count
:
(def map1 '{A: (1 2 3) B: (1 2) C: (5) } )
Sort by (count (vals map1))
=> {C: (5) B: (1 2) A: (1 2 3)}
Is there any straight-forward way to accomplish that? Many thanks!
Upvotes: 0
Views: 136
Reputation: 7568
Use sort-by with anonymous function:
(def map1 '{:A (1 2 3) :B (1 2) :C (5)})
(sort-by (fn [e] (count (val e))) map1)
(sort-by #(count (val %)) map1)
or with composition:
(sort-by (comp count val) map1)
=> ([:C (5)] [:B (1 2)] [:A (1 2 3)])
Upvotes: 3