summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2007-12-31 17:24:43 +0000
committerRich Hickey <richhickey@gmail.com>2007-12-31 17:24:43 +0000
commitad7a155a4950c93dfdc8c7168b77e8f73dba7e1e (patch)
tree57feeed96afce2adaddfba476b6680fa107dc286
parentc8ecaff2aa9e56de24fad89566d43affaf5a368b (diff)
added comparator, changed order of args to sort with comparator, added sort-by with comparator
-rw-r--r--src/boot.clj14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/boot.clj b/src/boot.clj
index 6c6f4f39..1325f4d4 100644
--- a/src/boot.clj
+++ b/src/boot.clj
@@ -601,20 +601,26 @@
(when line
(lazy-cons line (line-seq rdr)))))
+(defn comparator [pred]
+ (fn [x y] (cond (pred x y) -1 (pred y x) 1 :t 0)))
+
(defn sort
([#^java.util.Collection coll]
(when (and coll (not (. coll (isEmpty))))
(let [a (. coll (toArray))]
(. java.util.Arrays (sort a))
(seq a))))
- ([#^java.util.Collection coll #^java.util.Comparator comp]
+ ([#^java.util.Comparator comp #^java.util.Collection coll]
(when (and coll (not (. coll (isEmpty))))
(let [a (. coll (toArray))]
(. java.util.Arrays (sort a comp))
(seq a)))))
-(defn sort-by [keyfn #^java.util.Collection coll]
- (sort coll (fn [x y] (. (keyfn x) (compareTo (keyfn y))))))
+(defn sort-by
+ ([keyfn coll]
+ (sort (fn [x y] (. #^Comparable (keyfn x) (compareTo (keyfn y)))) coll))
+ ([keyfn #^java.util.Comparator comp coll]
+ (sort (fn [x y] (. comp (compare (keyfn x) (keyfn y)))) coll)))
;; evaluation
@@ -957,7 +963,7 @@
assoc dissoc find keys vals merge merge-with
scan touch
key val
- line-seq sort sort-by
+ line-seq sort sort-by comparator
rseq sym name namespace locking .. ->
defmulti defmethod remove-method
binding find-var