Thanks. What a newbie question.
在 2019年5月6日星期一 UTC+8上午11:34:36,se...@corfield.org写道:
>
> (sort-by #{:age} …) will use the set #{:age} as the keyfn, and both
> (#{:age} {:age 3, :name “luo”}) and (#{:age} {:age 1, :name “sheng”}) both
> return :age – because both maps contain the key in the set.
(sort-by #{:age} …) will use the set #{:age} as the keyfn, and both (#{:age} {:age 3, :name “luo”}) and (#{:age} {:age 1, :name “sheng”}) both return :age – because both maps contain the key in the set. As far as sort-by is concerned, both hash maps compare equal. What you want is (sort-by :age …)
clojure.core/sort-by
([keyfn coll] [keyfn comp coll])
Returns a sorted sequence of the items in coll, where the sort
order is determined by comparing (keyfn item). If no comparator is
supplied, uses compare. comparator must implement
java.util.Comparator. Guaranteed to be stable: equal
Hey there,
in my lein repl,
(sort-by #{:age} [{:age 3,:name "luo"},{:age 1,:name "sheng"}])
returns
({:age 3, :name "luo"} {:age 1, :name "sheng"})
rather than
({:age 1, :name "sheng"}, {:age 3, :name "luo"}).
Why?
--
You received this message because you are subscribed to the