简体   繁体   English

在向量的Clojure地图中排序向量

[英]Sorting vectors in Clojure map of vectors

I have a map of vectors, like this: 我有一个矢量地图,像这样:

{2 ["a" "c" "b"], 1 ["z" "y" "x"]}

I want to get a map that is sorted by keys, and then each corresponding vector is also sorted, like this: 我想得到一个按键排序的地图,然后每个相应的矢量也被排序,如下所示:

{1 ["x" "y" "z"], 2 ["a" "b" "c"]}

I know I can sort by keys by doing (into (sorted-map) themap) , and I know that I can supply a transducer to into , but I'm coming up short as to exactly how the transducer should look. 我知道我可以通过按键做排序(into (sorted-map) themap)我知道我可以提供一个换能器into ,但我短上来的换能器究竟应该怎么看。 Here's a transducer I've tried: 这是我试过的换能器:

(defn xform [entry]
  (vector (first entry) (vec (sort (second entry)))))

However, when I try to apply it to my map, I get this exception: 但是,当我尝试将其应用到我的地图时,我得到以下异常:

java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.core$conj__4345

How can I get this to work? 我怎样才能让它发挥作用? Is there a better way then using into with a transducer? 有没有更好的方法,然后使用into与换能器?

Like this: 像这样:

(into (sorted-map)
      (map (fn [[k v]] [k (vec (sort v))]))
      {2 ["a" "c" "b"], 1 ["z" "y" "x"]})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM