Skip to content

Instantly share code, notes, and snippets.

@ertugrulcetin
Last active April 7, 2017 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ertugrulcetin/fa0b53f12226ec839e732081c8b11cc4 to your computer and use it in GitHub Desktop.
Save ertugrulcetin/fa0b53f12226ec839e732081c8b11cc4 to your computer and use it in GitHub Desktop.
with multi-comp you can combine comp fns -> [:age :count :size :weight]
(def data [{:v 12, :a 10} {:v 21, :a 113} {:v 1, :a 2} {:v 12, :a 223} {:v 100, :a 23} {:v 1, :a 113}])
(defn multi-comp
([fns a b]
(multi-comp fns < a b))
([[f & others :as fns] order a b]
(if (seq fns)
(let [result (compare (f a) (f b))
f-result (if (= order >) (* -1 result) result)]
(if (= 0 f-result)
(recur others order a b)
f-result))
0)))
(sort #(multi-comp [:a :v] > %1 %2) data)
;or
(sort #(multi-comp [:a :v] %2 %1) data)
;;=> ({:v 12, :a 223} {:v 21, :a 113} {:v 1, :a 113} {:v 100, :a 23} {:v 12, :a 10} {:v 1, :a 2})
(sort #(multi-comp [:a :v] < %1 %2) data)
;or
(sort #(multi-comp [:a :v] %1 %2) data)
;;=> ({:v 1, :a 2} {:v 12, :a 10} {:v 100, :a 23} {:v 1, :a 113} {:v 21, :a 113} {:v 12, :a 223})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment