philipbohun.com

Glojure by Example: Sorting

sort returns a sorted sequence. It does not modify the input. Numbers sort numerically. Strings sort lexicographically.

(println (sort [5 2 9 1]))
(println (sort ["pear" "apple" "grape"]))

The Go sort package sorts host slices in place. sort.Strings rearranges a []string. vec copies it back to a vector for printing.

(def fruits (go/append (go/make (go/slice-of go/string))
                       "pear" "apple" "grape"))
(sort.Strings fruits)
(println (vec fruits))

Clojure sort returns a new sequence. sort.Strings mutates the slice.

$ glj sorting.glj
(1 2 5 9)
(apple grape pear)
[apple grape pear]

Next example: Sorting by Functions.