Skip to content

Instantly share code, notes, and snippets.

@skatenerd
Last active August 29, 2015 13:56
Show Gist options
  • Save skatenerd/8814785 to your computer and use it in GitHub Desktop.
Save skatenerd/8814785 to your computer and use it in GitHub Desktop.
lol
(defn sort-by-lambdas [lambdas to-sort]
(sort-by
#(mapv (fn [lambda] (lambda %)) lambdas)
to-sort))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;test
(describe "sorting"
(it "sorts by lowercased name"
(let [people [{:name "aaa"} {:name "BBB"}]]
(should=
["aaa" "BBB"]
(map :name (sort-by-lambdas [#(clojure.string/lower-case (:name %))] people)))))
(it "prioritizes one field over another"
(let [first-person {:age 22 :children 1}
second-person {:age 22 :children 100}
third-person {:age 30 :children 0}]
(should=
[first-person second-person third-person]
(sort-by-lambdas [:age :children] [first-person second-person third-person]))
(should=
[third-person first-person second-person]
(sort-by-lambdas [:children :age] [first-person second-person third-person])))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment