Darrell Brogdon
Darrell Brogdon

Reputation: 6973

How to maintain order when iterating over a Map in Clojure?

I have the following map:

(def tmp-cust-data {:customers
                    {:january [1 0 2 0 3 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
                     :february [0 1 0 2 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
                     :march [0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
                     :april [1 0 2 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
                     :may [1 0 2 0 3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
                     :june [1 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
                     :july [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
                     :august [1 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
                     :september [1 0 2 0 3 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
                     :october [1 0 2 0 3 0 4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
                     :november [1 0 2 0 3 0 4 0 5 0 6 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
                     :december [1 0 2 0 3 0 4 0 5 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]}})

When I do something like doseq over that map, I notice the order isn't preserved:

(doseq [[k v] (:customers tmp-cust-data)] (println k))

Is there a proper way to iterate over a map while preserving the order?

Upvotes: 1

Views: 394

Answers (2)

Sean Corfield
Sean Corfield

Reputation: 6666

A hash map is inherently unordered by definition. You can choose an order for your keys and you can then iterate over those keys in order and access the hash map values, but the hash map itself cannot possibly have "an order".

Upvotes: 3

akond
akond

Reputation: 16035

Use an ordered version of the map.

(def tmp-cust-data {:customers
                    (flatland.ordered.map/ordered-map
                        [[:january [1 0 2 0 3 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
                         [:february [0 1 0 2 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
                         [:march [0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
                         [:april [1 0 2 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
                         [:may [1 0 2 0 3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
                         [:june [1 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
                         [:july [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
                         [:august [1 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
                         [:september [1 0 2 0 3 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
                         [:october [1 0 2 0 3 0 4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
                         [:november [1 0 2 0 3 0 4 0 5 0 6 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
                         [:december [1 0 2 0 3 0 4 0 5 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]])})

Upvotes: 3

Related Questions