Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.