You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/md/articles/language/collections_and_sequences.md
+80-1
Original file line number
Diff line number
Diff line change
@@ -790,7 +790,7 @@ Do not confuse `empty?` with `empty`. This can be a source of great confusion:
790
790
791
791
### contains?
792
792
793
-
`contains` returns true if the provided *key* is present in a collection. `contains` is similar to `get` in that vectors treat the key as an index. `contains` will always return false for lists.
793
+
`contains?` returns true if the provided *key* is present in a collection. `contains?` is similar to `get` in that vectors treat the key as an index. `contains?` does not work for lists.
794
794
795
795
```klipse-clojure
796
796
(contains? {:a 1 :b 2 :c 3} :c)
@@ -996,6 +996,85 @@ returns falsey. This is because if the item is present in the set it is returned
996
996
;; ⇒ (:things :someone nil false :pigeons)
997
997
```
998
998
999
+
### sort, sort-by
1000
+
1001
+
`sort` and `sort-by` are flexible higher-order functions for sorting sequential collections like lists and vectors. Both take an optional `Comparator` which defaults to `compare`, and `sort-by` takes a function that transforms each value before comparison.
1002
+
1003
+
#### sort
1004
+
1005
+
`sort` using the default comparator can handle numbers...
1006
+
```klipse-clojure
1007
+
(sort [0.0 -1 1.3 nil 0.18 7])
1008
+
;; ⇒ (nil -1 0.0 0.18 1.3 7)
1009
+
```
1010
+
... and strings
1011
+
```klipse-clojure
1012
+
(sort ["the case matters" "lexicographic ordering" "The case matters" nil "%%"])
1013
+
;; ⇒ (nil "%%" "The case matters" "lexicographic ordering" "the case matters")
1014
+
```
1015
+
... and vectors whose elements are element-wise comparable
; `compare` doesn't know how to compare Strings and numbers
1023
+
(sort [5 1.0 "abc"])
1024
+
;; Execution error (ClassCastException)...
1025
+
;; class java.lang.Double cannot be cast to class java.lang.String
1026
+
```
1027
+
1028
+
In order to do more complicated sorting, we can create our own `Comparator`. There's a wealth of information
1029
+
about comparators in the [clojure.org comparators guide](https://www.clojure.org/guides/comparators), but for now, one possible comparator is a
1030
+
function that takes two arguments and returns a negative, positive, or zero integer when the first argument is 'less than', 'greater than', or equal to (respectively) the second argument.
1031
+
1032
+
```klipse-clojure
1033
+
(letfn [(strings-before-numbers
1034
+
[x y]
1035
+
(cond
1036
+
; string is 'less than' number
1037
+
(and (string? x) (number? y)) -1
1038
+
; number is 'greater than' string
1039
+
(and (number? x) (string? y)) 1
1040
+
; otherwise we can use `compare`
1041
+
:else (compare x y)))]
1042
+
(sort strings-before-numbers [1 0.0 nil "abc"]))
1043
+
;; ⇒ (nil "abc" 0.0 1)
1044
+
```
1045
+
1046
+
A common way to reverse a sort is to `comp` the `-` function with a comparator that returns a number, which effectively
0 commit comments