compare

functionsince v0.0-927 clojure.core/compareEdit
(compare x y)

Details:

Comparator.

Returns a negative number, zero, or a positive number when x is logically "less than", "equal to", or "greater than" y.

Uses IComparable if available and google.array.defaultCompare for objects of the same type. nil is treated as a special case and is always less than any other object.


Examples:

(compare 10 12)
;;=> -1

(compare 12 10)
;;=> 1

(compare 10 10)
;;=> 0

(compare 10 nil)
;;=>  1

(compare 10 (list 1 2 3))
;; Error: compare on non-nil objects of different types

See Also:


Source docstring:
Comparator. Returns a negative number, zero, or a positive number
 when x is logically 'less than', 'equal to', or 'greater than'
 y. Uses IComparable if available and google.array.defaultCompare for objects
of the same type and special-cases nil to be less than any other object.
Source code @ clojurescript:src/main/cljs/cljs/core.cljs
(defn ^number compare
  [x y]
  (cond
   (identical? x y) 0

   (nil? x) -1

   (nil? y) 1

   (number? x) (if (number? y)
                 (garray/defaultCompare x y)
                 (throw (js/Error. (str "Cannot compare " x " to " y))))

   (satisfies? IComparable x)
   (-compare x y)

   :else
   (if (and (or (string? x) (array? x) (true? x) (false? x))
            (identical? (type x) (type y)))
     (garray/defaultCompare x y)
     (throw (js/Error. (str "Cannot compare " x " to " y))))))