Reputation: 832
I define a map in my code like:
(def templates {:list1 {:create_time "create_time"
:recharge_amount "recharge"
:invest_amount "invest"
;; something else
}
:list2 {:create_time "ct"
;; something else
}
;;something else
})
I want the map keep the order by what I am defined. How to solve it?
Upvotes: 16
Views: 6222
Reputation: 6073
If your data won't change, you can use array-map
:
(def templates
(array-map
:list1 {}
:list2 {}
:list3 {}))
(seq templates)
;; => ([:list1 {}] [:list2 {}] [:list3 {}])
(keys templates)
;; => (:list1 :list2 :list3)
Why the constraint with constant data, you ask? Well, an array-map might turn into a hash-map after basically any modification operation (if its contents grow too big to offer efficient lookups).
Alternatively, if you're open to external dependencies you could see if amalloy/ordered fits your needs.
Upvotes: 16