Skip to content

Commit 60d90fa

Browse files
committed
Fix sort-by
Signed-off-by: James Hamlin <jfhamlin@gmail.com>
1 parent 117117d commit 60d90fa

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

pkg/stdlib/glojure/core.glj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3106,7 +3106,7 @@
31063106
([keyfn coll]
31073107
(sort-by keyfn compare coll))
31083108
([keyfn ^java.util.Comparator comp coll]
3109-
(sort (fn [x y] (. comp (compare (keyfn x) (keyfn y)))) coll)))
3109+
(sort (fn [x y] (comp (keyfn x) (keyfn y))) coll)))
31103110

31113111
(defn dorun
31123112
"When lazy sequences are produced via functions that have side

scripts/rewrite-core/rewrite.clj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,9 @@
944944
(sexpr-replace '(. java.util.Arrays (sort a comp))
945945
'(github.com$glojurelang$glojure$pkg$lang.SortSlice a comp))
946946

947+
;; comparators are simple functions in Glojure
948+
(sexpr-replace '(. comp (compare (keyfn x) (keyfn y)))
949+
'(comp (keyfn x) (keyfn y)))
947950
]))
948951

949952
(defn rewrite-core [zloc]

test/glojure/test_glojure/sort.glj

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
;; Tests for sort function
2-
;; Ensures Glojure behavior matches Clojure
2+
;; Ensures Glojure behavior matches Clojure as closely as possible
33

44
(ns glojure.test-glojure.sort
55
(:require [glojure.test :refer :all]
@@ -51,8 +51,7 @@
5151

5252
;; Sort by string length
5353
(is (= '("a" "bb" "ccc" "dddd")
54-
(sort (fn [a b] (compare (count a) (count b)))
55-
["ccc" "a" "dddd" "bb"])))
54+
(sort-by count ["ccc" "a" "dddd" "bb"])))
5655

5756
;; Sort maps by a specific key
5857
(let [data [{:name "John" :age 30}
@@ -202,15 +201,15 @@
202201
;; Lists are not comparable
203202
(is (thrown? go/any (compare '(1 2) '(1 2))))
204203
(is (thrown? go/any (sort ['(1 2) '(3 4)])))
205-
204+
206205
;; Maps are not comparable
207206
(is (thrown? go/any (compare {:a 1} {:b 2})))
208207
(is (thrown? go/any (sort [{:a 1} {:b 2}])))
209-
208+
210209
;; Sets are not comparable
211210
(is (thrown? go/any (compare #{1 2} #{3 4})))
212211
(is (thrown? go/any (sort [#{1 2} #{3 4}])))
213-
212+
214213
;; Mixed incompatible types
215214
(is (thrown? go/any (compare 1 :a)))
216215
(is (thrown? go/any (compare "string" 'symbol)))
@@ -222,15 +221,15 @@
222221
(is (= -1 (compare [1 2] [1 3])))
223222
(is (= 1 (compare [1 3] [1 2])))
224223
(is (= 0 (compare [1 2 3] [1 2 3])))
225-
224+
226225
;; Shorter vectors are less than longer vectors with same prefix
227226
(is (= -1 (compare [1 2] [1 2 3])))
228227
(is (= 1 (compare [1 2 3] [1 2])))
229-
228+
230229
;; Nested vectors
231230
(is (= -1 (compare [[1 2] [3 4]] [[1 2] [3 5]])))
232231
(is (= 0 (compare [[1 2] [3 4]] [[1 2] [3 4]])))
233-
232+
234233
;; SubVectors behave like vectors
235234
(let [v [1 2 3 4 5]
236235
sv1 (subvec v 1 3) ; [2 3]
@@ -243,11 +242,11 @@
243242
;; No namespace < with namespace
244243
(is (= -1 (compare 'x 'a/x)))
245244
(is (= 1 (compare 'a/x 'x)))
246-
245+
247246
;; Different namespaces
248247
(is (= -1 (compare 'a/x 'b/x)))
249248
(is (= 1 (compare 'b/x 'a/x)))
250-
249+
251250
;; Same namespace, different names
252251
(is (= -1 (compare 'ns/a 'ns/b)))
253252
(is (= 1 (compare 'ns/b 'ns/a)))))

0 commit comments

Comments
 (0)