(sort-by keyfn coll)
(sort-by keyfn comp coll)
Returns a sorted sequence of the items in coll, where the sort
order is determined by comparing (keyfn item). Comp can be
boolean-valued comparison funcion, or a -/0/+ valued comparator.
Comp defaults to compare.
Example
Sort using a specific keyword
(sort-by
:year
<
[{:name "Lisp", :year 1959}
{:name "Fortran", :year 1957}
{:name "Smalltalk", :year 1972}])
Sort numbers lexicographically
(sort-by str [5 18 83 23 40])
Sort with a function
(sort-by count ["oh" "this" "is" "super" "awesome"])
Sort using multiple criteria
(sort-by (juxt count str) ["oh" "this" "is" "super" "awesome"])
Sort using multiple criteria
(sort-by (juxt :a :b) [{:a 1, :b 3} {:a 1, :b 2} {:a 2, :b 1}])
Source
(defn
sort-by
"Returns a sorted sequence of the items in coll, where the sort\n order is determined by comparing (keyfn item). Comp can be\n boolean-valued comparison funcion, or a -/0/+ valued comparator.\n Comp defaults to compare."
([keyfn coll] (sort-by keyfn compare coll))
([keyfn comp coll]
(sort (fn [x y] ((fn->comparator comp) (keyfn x) (keyfn y))) coll)))