Common Methods for clojure map data structure:
Map sorting
(Println (sorted-map-by> 1 "a", 2 "B", 3 "c"); {3 c, 2 B, 1 };; take one equal value (let [results {: A 1: B 2: C 2: D 5: E 1: F 1}] (println (into (sorted-map-by (fn [key1 key2] (compare (get results key2) (get results key1 )))) results); {: D 5,: C 2,: A 1}; all unequal values (let [results {: A 1: B 2: F 4: E 3: D 6: C 5}] (println (into (sorted-map-by (fn [key1 key2] (compare (get results key2) (get results key1) results); {: D 6,: C 5,: F 4,: E 3,: B 2 ,: A 1}; value Ascending Order (let [results {: A 1: B 2: F 4: E 3: D 6: C 5}] (println (into (sorted-map-by (fn [key1 key2] (compare [(get results key1) key1] [(get results key2) key2]) results); {: A 1,: B 2,: E 3,: F 4,: C 5,: D 6 };; value in descending order (let [results {: A 1: B 2: F 4: E 3: D 6: C 5}] (println (into (sorted-map-by (fn [key1 key2] (compare [(get results key2) key2] [(get results key1) key1]) results); {: D 6,: C 5,: F 4,: E 3,: B 2,: A 1 };; value Ascending Order (let [results {: A 1: B 2: F 4: E 3: D 6: C 5}] (println (into (sorted-map-by (fn [key1 key2] (compare [(get results: key1) key1] [(get results: key2) key2]) results); {: A 1,: B 2,: C 5,: D 6,: E 3,: F 4 };; value in descending order (let [results {: A 1: B 2: F 4: E 3: D 6: C 5}] (println (into (sorted-map-by (fn [key1 key2] (compare [(get results: key2) key2] [(get results: key1) key1]) results); {: F 4,: E 3,: D 6,: C 5,: B 2,: A 1 };; default Ascending Order (println (into (sorted-map) {: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-24 0,: 2013-12-18 0 ,: 2013-12-19 3600}); {: 2013-12-18 0,: 2013-12-19 3600,: 2013-12-20 3600,: 2013-12-21 0,: 2013-12-22 0,: 2013-12-23 0,: 2013-12-24 0 };; take the last one after sorting (println (last (into (sorted-map) {: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-24 0 ,: 2013-12-18 0,: 2013-12-19 3600}); [: 2013-12-24 0]; sort custom keys in descending order (let [results {: 2013-12-23 0,: 2013-12-22 0 ,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-24 0,: 2013-12-18 0 ,: 3600}] (println (into (sorted-map-by (fn [key1 key2] (compare [(get results: key2) key2] [(get results: key1) key1]) results); {: 2013-12-24 0,: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-19 3600,: 2013-12-18 0}
Values after sorting
; Sort dates first, then take the previous num Day (defn take-sort-data [data num] (let [result-sorted (into (sorted-map-by (fn [key1 key2] (compare [( get data: key2) key2] [(get data: key1) key1]) data );; result (select-keys result-sorted (take num (keys result-sorted)] (println "data =" data) (println "result-sorted =" result-sorted) (println "result =" result) (println "json/write-str result =" (json/write-str result) (let [data {: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0 ,: 3600,: 0,: 2013-12-18 0,: 2013-12-19 3600}] (take-sort-data 6) (take-sort-data 4 ));; (take-sort-data 6)'s result; data = {: 2013-12-24 0,: 2013-12-23 0,: 2013-12-20 3600,: 2013-12-21 0,: 2013-12-22 0,: 2013-12-18 0,: 2013-12-19 3600}; result-sorted = {: 2013-12-24 0,: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-19 3600,: 2013-12-18 0 }; result = {: 2013-12-19 3600,: 2013-12-20 3600,: 2013-12-21 0,: 2013-12-22 0,: 2013-12-23 0,: 2013-12-24 0}; json/write-str result = {"2013-12-19 ": 3600, "2013-12-20": 3600, "2013-12-21": 0, "2013-12-22": 0, "2013-12-23": 0, "2013-12-24": 0 };;; (take-sort-data 4)'s result; data = {: 2013-12-24 0,: 2013-12-23 0,: 2013-12-20 3600,: 2013-12-21 0,: 2013-12-22 0,: 2013-12-18 0,: 2013-12-19 3600}; result-sorted = {: 2013-12-24 0,: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-19 3600,: 2013-12-18 0 }; result = {: 2013-12-21 0,: 2013-12-22 0,: 2013-12-23 0,: 2013-12-24 0}; json/write-str result = {"2013-12-21": 0, "2013-12-22": 0, "2013-12-23": 0, "2013-12-24": 0}
Sorted-map
Sorted-map-
Compare