Created
August 8, 2012 00:38
-
-
Save jjcomer/3290950 to your computer and use it in GitHub Desktop.
Ordering columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def c-order | |
(let [order [:id :title :author :date :price]] | |
(zipmap order (range (count order))))) | |
(defn column-sort | |
[k1 k2] | |
(compare (get c-order k1 -1) | |
(get c-order k2 -1))) | |
(def test-data | |
(into (sorted-map-by column-sort) | |
{:price "3.99" :author "John D." :title "A Sorted Map" | |
:id 12345 :date (java.util.Date.)})) | |
user=> test-data | |
{:id 12345, :title "A Sorted Map", :author "John D.", :date #inst "2012-08-08T00:18:50.219-00:00", :price "3.99"} | |
user=> (vec test-data) | |
[[:id 12345] [:title "A Sorted Map"] [:author "John D."] [:date #inst "2012-08-08T00:18:50.219-00:00"] [:price "3.99"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
zipmap can take a lazy-seq so you can just do (zipmap order (range))