Reputation: 4007
To sort a vector in clojure
(sort [ 2 3 1 4])
returns a list (sorted)
(1 2 3 4)
I can turn it back into the vector by
(into [] (sort [ 2 3 1 4]))
which is kind of inconvenient.
But if I sort a map/dictionary in clojure
(sort {2 "" 3 "" 1 "" 4 ""})
the return
([1 ""] [2 ""] [3 ""] [4 ""])
How do I turn it back into a sorted map? Or is there a better sort function that keeps the type/shape of the input?
Upvotes: 3
Views: 327
Reputation: 4756
I dont know of a general way to always keep the original type/shape.
In case of a sorted map, its important to know that a regular map in clojure has no order (at least nothing you can or should rely on). For that clojure has sorted-map
. There is also a useful function sorted-map-by
that allows you to specify your own ordering.
So, in your example:
(into (sorted-map) {2 "" 3 "" 1 "" 4 ""})
;; {1 "", 2 "", 3 "", 4 ""}
Upvotes: 3