comparator

clojure.core

  • (comparator pred)
Returns an implementation of java.util.Comparator based upon pred.

1 Example top

  • ;; simple example to create an ArrayList, initially [1,2,0]
    ;; and sort it in descending order
    
    user=> (def a (doto (new java.util.ArrayList) (.add 1) (.add 2) (.add 0)))
    #'user/a
    user=> (def compx (comparator (fn [x y] (> x y))))
    #'user/compx
    user=> (java.util.Collections/sort a compx)
    nil
    user=> a
    #<ArrayList [2, 1, 0]>
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:2679 top

(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)))
Vars in clojure.core/comparator:
Used in 0 other vars

Comments top

No comments for comparator. Log in to add a comment.