Skip to content

Instantly share code, notes, and snippets.

@jjcomer
Created August 8, 2012 00:38
Show Gist options
  • Save jjcomer/3290950 to your computer and use it in GitHub Desktop.
Save jjcomer/3290950 to your computer and use it in GitHub Desktop.
Ordering columns
(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"]]
@trevorbernard
Copy link

zipmap can take a lazy-seq so you can just do (zipmap order (range))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment