store/[org.clojure/clojure "1.8.0"] clj::clojure.core/comparator

Official Documentation

Add notes
Arities
==================================================
   [pred]

Docstring
==================================================
  Returns an implementation of java.util.Comparator based upon pred.

Source

(defn comparator
  "Returns an implementation of java.util.Comparator based upon pred."
  {:added "1.0"
   :static true}
  [pred]
    (fn [x y]
      (cond (pred x y) -1 (pred y x) 1 :else 0)))

Example 1

Edit
;; simple example to create an ArrayList, initially [1,2,0]
;; and sort it in descending order

(def a (new java.util.ArrayList [1 2 0]))
;; => #'user/a

(def compx (comparator (fn [x y] (> x y))))
;; => #'user/compx

(java.util.Collections/sort a compx)
;; => nil

a
;; => #<ArrayList [2, 1, 0]>

;; Note however that 'comparator' is rarely (never?) needed because if
;; the fn returns a boolean, the .compare implementation Clojure provides
;; causes it to behave the same as if 'comparator' were wrapped around it:

(sort (comparator (fn [x y] (> x y))) [1 2 0]) ;=> (2 1 0)
(sort (fn [x y] (> x y)) [1 2 0])              ;=> (2 1 0)
(sort > [1 2 0])                               ;=> (2 1 0)
(sort < [1 2 0])                               ;=> (0 1 2)

Uses on crossclj