Function: sort-regexp-fields
sort-regexp-fields is an autoloaded, interactive and byte-compiled
function defined in sort.el.gz.
Signature
(sort-regexp-fields REVERSE RECORD-REGEXP KEY-REGEXP BEG END)
Documentation
Sort the text in the region lexicographically.
If called interactively, prompt for two regular expressions, RECORD-REGEXP and KEY-REGEXP.
RECORD-REGEXP specifies the textual units to be sorted.
For example, to sort lines, RECORD-REGEXP would be "^.*$".
KEY-REGEXP specifies the part of each record (i.e. each match for
RECORD-REGEXP) to be used for sorting.
If it is "\\\\digit", use the digit'th "\\\\(...\\\\)"
match field specified by RECORD-REGEXP.
If it is "\\\\&", use the whole record.
Otherwise, KEY-REGEXP should be a regular expression with which
to search within the record. If a match for KEY-REGEXP is not
found within a record, that record is ignored.
With a negative prefix arg, sort in reverse order.
The variable sort-fold-case determines whether alphabetic case affects
the sort order.
For example: to sort lines in the region by the first word on each line
starting with the letter "f",
RECORD-REGEXP would be "^.*$" and KEY would be "\\\\=\\<f\\\\w*\\\\>"
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/sort.el.gz
;;;###autoload
(defun sort-regexp-fields (reverse record-regexp key-regexp beg end)
"Sort the text in the region lexicographically.
If called interactively, prompt for two regular expressions,
RECORD-REGEXP and KEY-REGEXP.
RECORD-REGEXP specifies the textual units to be sorted.
For example, to sort lines, RECORD-REGEXP would be \"^.*$\".
KEY-REGEXP specifies the part of each record (i.e. each match for
RECORD-REGEXP) to be used for sorting.
If it is \"\\\\digit\", use the digit'th \"\\\\(...\\\\)\"
match field specified by RECORD-REGEXP.
If it is \"\\\\&\", use the whole record.
Otherwise, KEY-REGEXP should be a regular expression with which
to search within the record. If a match for KEY-REGEXP is not
found within a record, that record is ignored.
With a negative prefix arg, sort in reverse order.
The variable `sort-fold-case' determines whether alphabetic case affects
the sort order.
For example: to sort lines in the region by the first word on each line
starting with the letter \"f\",
RECORD-REGEXP would be \"^.*$\" and KEY would be \"\\\\=\\<f\\\\w*\\\\>\""
;; using negative prefix arg to mean "reverse" is now inconsistent with
;; other sort-.*fields functions but then again this was before, since it
;; didn't use the magnitude of the arg to specify anything.
(interactive "P\nsRegexp specifying records to sort: \n\
sRegexp specifying key within record: \nr")
(cond ((or (equal key-regexp "") (equal key-regexp "\\&"))
(setq key-regexp 0))
((string-match "\\`\\\\[1-9]\\'" key-regexp)
(setq key-regexp (- (aref key-regexp 1) ?0))))
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(let (sort-regexp-record-end
(sort-regexp-fields-regexp record-regexp))
(re-search-forward sort-regexp-fields-regexp nil t)
(setq sort-regexp-record-end (point))
(goto-char (match-beginning 0))
(sort-subr reverse
'sort-regexp-fields-next-record
(lambda ()
(goto-char sort-regexp-record-end))
(lambda ()
(let ((n 0))
(cond ((numberp key-regexp)
(setq n key-regexp))
((re-search-forward
key-regexp sort-regexp-record-end t)
(setq n 0))
(t (throw 'key nil)))
(condition-case ()
(cons (match-beginning n)
(match-end n))
;; if there was no such register
(error (throw 'key nil))))))))))