You're viewing version 1.2.0 of compare. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

compare

clojure.core

  • (compare x y)
Comparator. Returns a negative number, zero, or a positive number
when x is logically 'less than', 'equal to', or 'greater than'
y. Same as Java x.compareTo(y) except it also works for nil, and
compares numbers and collections in a type-independent manner. x
must implement Comparable

1 Example top

  • ;; various examples
    ;; comparing vectors of different sizes does not work as you may expect
    ;; the longer vector is always "greater" regardless of contents 
    
    user=> (compare [0 1 2] [0 1 2])
    0
    user=> (compare [1 2 3] [0 1 2 3])
    -1
    user=> (compare [0 1 2] [3 4])
    1
    user=> (compare nil [1 2 3])
    -1
    user=> (compare [1 2 3] nil)
    1
    user=> (compare "abc" "def")
    -3
    user=> (compare "abc" "abd")
    -1
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:682 top

(defn compare
  "Comparator. Returns a negative number, zero, or a positive number
  when x is logically 'less than', 'equal to', or 'greater than'
  y. Same as Java x.compareTo(y) except it also works for nil, and
  compares numbers and collections in a type-independent manner. x
  must implement Comparable"
  {
   :inline (fn [x y] `(. clojure.lang.Util compare ~x ~y))
   :added "1.0"}
  [x y] (. clojure.lang.Util (compare x y)))
Vars in clojure.core/compare: defn
Used in 3 other vars sort-by sort concordant?

Comments top

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