Elisp: Sequence, Sort, Reverse

By Xah Lee. Date: . Last updated: .

sort

sort

(sort SEQ PREDICATE)

  • sort a Sequence
  • original is destroyed.
(setq xx (list 2 4 1 9 5))

(sort xx '>)
;; (9 5 4 2 1)

xx
;; (9 5 4 2 1)

;; note, the original list may be destroyed

seq-sort

(seq-sort PRED SEQUENCE)

(setq xx (list 2 4 1 9 5))

(seq-sort '> xx)
;; (9 5 4 2 1)

xx
;; (2 4 1 9 5)
;; original unchanged
seq-sort-by

(seq-sort-by FUNCTION PRED SEQUENCE)

Sort sequence, by the value of a function applied to each element.

FUNCTION takes one arg.

(setq xx (list "a-9" "b-3" "c-2" ))

;; sort by the number in the string
(seq-sort-by
 (lambda (x) (string-to-number (substring x 2)))
 '<
 xx)
;; ("c-2" "b-3" "a-9")

xx
;; ("a-9" "b-3" "c-2")

Reverse

;; example showing how nreverse destroys the original value

(setq xx (number-sequence 1 5))
(setq yy (nreverse xx))

xx
;; (1)

yy
;; (5 4 3 2 1)

Elisp, sequence functions