;; < 和 > 只接受数字,无法扩展。
;; 自有值的排序要通过 compare,而 compare 已支持向量,
;; 所以一个“键向量”就能完成全部工作
(defrecord Point [x y])
(defn p-compare [a b]
(compare [(:x a) (:y a)] [(:x b) (:y b)]))
(def p1 (->Point 1 2))
(def p2 (->Point 2 3))
(println "b1 is" (pos? (p-compare p1 p2)))
;; b1 is false
(println "b2 is" (neg? (p-compare p1 p2)))
;; b2 is true
;; 同样的规则也能给整个集合排序
(def sorted (sort-by (juxt :x :y) [p2 p1 (->Point 0 9)]))
(println "sorted is" (mapv (juxt :x :y) sorted))