|
11 | 11 | (is (= '(1 2 3 4 5) (sort [3 1 4 2 5]))) |
12 | 12 | (is (= '(1 1 3 4 5) (sort [3 1 4 1 5]))) ; duplicates preserved |
13 | 13 | (is (= '(1.0 2.5 3 4.7) (sort [4.7 1.0 3 2.5]))) ; mixed numeric types |
| 14 | + (is (= '(-5 0 1.5 2 3.14 10) (sort [3.14 2 1.5 10 -5 0]))) |
14 | 15 |
|
15 | 16 | ;; Strings |
16 | 17 | (is (= '("apple" "banana" "cherry") (sort ["cherry" "apple" "banana"]))) |
|
251 | 252 | (is (= -1 (compare 'ns/a 'ns/b))) |
252 | 253 | (is (= 1 (compare 'ns/b 'ns/a))))) |
253 | 254 |
|
| 255 | +(deftest sort-by-clojuredocs-examples |
| 256 | + (testing "Examples from ClojureDocs sort-by documentation" |
| 257 | + |
| 258 | + (let [words ["banana" "apple" "cherry" "date"]] |
| 259 | + (is (= '("date" "apple" "banana" "cherry") |
| 260 | + (sort-by count words)))) |
| 261 | + |
| 262 | + (let [words ["banana" "apple" "cherry" "date"]] |
| 263 | + (is (= (sort-by count > words) |
| 264 | + '("banana" "cherry" "apple" "date")))) |
| 265 | + |
| 266 | + (let [people [{:name "Alice" :age 30 :city "NYC"} |
| 267 | + {:name "Bob" :age 25 :city "LA"} |
| 268 | + {:name "Charlie" :age 35 :city "NYC"} |
| 269 | + {:name "David" :age 25 :city "LA"}]] |
| 270 | + (is (= (sort-by (juxt :city :age) people) |
| 271 | + '({:name "Bob" :age 25 :city "LA"} |
| 272 | + {:name "David" :age 25 :city "LA"} |
| 273 | + {:name "Alice" :age 30 :city "NYC"} |
| 274 | + {:name "Charlie" :age 35 :city "NYC"})))) |
| 275 | + |
| 276 | + (let [numbers [3 1 4 1 5 9 2 6]] |
| 277 | + (is (= '(3 9 6 1 4 1 5 2) |
| 278 | + (sort-by #(mod % 3) numbers)))) |
| 279 | + |
| 280 | + (let [items [nil "hello" 42 :keyword]] |
| 281 | + (is (= (sort-by str items) |
| 282 | + '(nil 42 :keyword "hello")))) |
| 283 | + |
| 284 | + (is (= (sort-by identity []) '())) |
| 285 | + (is (= (sort-by count []) '())) |
| 286 | + |
| 287 | + (is (= (sort-by identity [42]) '(42))) |
| 288 | + (is (= (sort-by count ["hello"]) '("hello"))))) |
| 289 | + |
254 | 290 | ;; Run tests |
255 | 291 | (run-tests) |
0 commit comments