If I've got a sequence of records defined by (defrecord Person [name age])
and I want to get the record of the person with the maximum age, is there an easier way to do this than
(reduce #(if (> (:age %1) (:age %2)) %1 %2) people)
That's the only way I've figured out to do it so far, but it seems like this has to be a common enough scenario that there must be some built-in library functions that make this easier and/or more generic.
clojure.core/max-key
is a suitable tool for the job.
(apply max-key :age [{:age 12} {:age 20} {:age 30}]) ;; -> {:age 30}
(last (sort-by :age [{:age 12} {:age 20} {:age 30}]))
sort-by uses compare
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With