(let [mymap (into {} (for [x (shuffle (rest (clojure.string/split "abcdefghijklmnopqrstuvwxyz" #"")))]
{x {:idx (rand-int 24)}}))]
(into (sorted-map-by (fn [k1 k2]
(compare [(get-in mymap [k1 :idx]) k1]
[(get-in mymap [k2 :idx]) k2])))
mymap))
或
(let [mymap (into {} (for [x (shuffle (rest (clojure.string/split "abcdefghijklmnopqrstuvwxyz" #"")))]
{x {:idx (rand-int 24)}}))]
(->> mymap
(sort-by (fn [[_ m]] (:idx m)))
(into (array-map))))
=> {" d" {:idx 22}," n" {:idx 22}," z" {:idx 14}," w" {:idx 11}," s" {:idx 17}," f" {:idx 20}," e" {:idx 19}," q" {:idx 12}," p" {:idx 10}," j" {:idx 0}," x" {:idx 20}," v" {:idx 14}," a" {:idx 1}," t" {:idx 13},"我" {:idx 21}," k" {:idx 16}," b" {:idx 23}," r" {:idx 3}," y" {:idx 18}," g" {:idx 0}," l" {:idx 16}," u" {:idx 20}," h" {:idx 10}," m" {:idx 16}," o" {:idx 11}," c" {:idx 4}}
如果我们将金额限制为8即可。
(let [mymap (into {} (for [x (shuffle (rest (clojure.string/split "abcdefghijklmnopqrstuvwxyz" #"")))]
{x (rand-int 24)}))]
(into (sorted-map-by (fn [k1 k2]
(compare [(get mymap k1) k1]
[(get mymap k2) k2])))
(take 8 mymap)))
{" Z" {:idx 1}," q" {:idx 6}," n" {:idx 7}," s" {:idx 7}," f" {:idx 9}," d" {:idx 17}," w" {:idx 18}," e" {:idx 21}}
相关联答案 0 :(得分:2)
您使用的是什么版本的clojurescript? cljs 0.0-3308中的第一个表单按预期保持排序顺序:
(println *clojurescript-version*) ;=> "0.0-3308"
(let [mymap (into {} (for [x (shuffle (rest (clojure.string/split "abcdefghijklmnopqrstuvwxyz" #"")))]
{x {:idx (rand-int 24)}}))]
(into (sorted-map-by (fn [k1 k2]
(compare [(get-in mymap [k1 :idx]) k1]
[(get-in mymap [k2 :idx]) k2])))
mymap))
;=> {"d" {:idx 0}, "i" {:idx 0}, "w" {:idx 5}, "n" {:idx 6}, "q" {:idx 9}, "y" {:idx 9}, "k" {:idx 10}, "m" {:idx 10}, "x" {:idx 10}, "c" {:idx 12}, "h" {:idx 12}, "l" {:idx 13}, "v" {:idx 14}, "s" {:idx 15}, "z" {:idx 16}, "j" {:idx 17}, "p" {:idx 17}, "t" {:idx 17}, "o" {:idx 18}, "r" {:idx 19}, "b" {:idx 20}, "e" {:idx 20}, "a" {:idx 21}, "f" {:idx 21}, "g" {:idx 22}, "u" {:idx 23}}
您使用的是版本早于2411 吗?这是第一个版本,其中包含对您链接的问题的答案中解释的问题的修复。它已在this commit上修复,该版本于2411版首次发布。