sort-by sorts by a key function. The keyword :age reads the age from each map. sort is stable, so two people of the same age keep their original order.
sort-by
:age
sort
(def people [{:name "ada" :age 36} {:name "bea" :age 20} {:name "cy" :age 36}]) (doseq [person (sort-by :age people)] (println (:name person) (:age person)))
Pass a comparator when the default order is wrong. > reverses a numeric key.
>
(println (sort-by identity > [1 5 2]))
Bea is youngest. Ada stays ahead of Cy.
$ glj sorting-by-functions.glj bea 20 ada 36 cy 36 (5 2 1)
Next example: String Functions.
A hands-on tour in the style of Go by Example. Glojure is a Clojure interpreter hosted on Go.