Function: ses-sort-column
ses-sort-column is an interactive and byte-compiled function defined
in ses.el.gz.
Signature
(ses-sort-column SORTER &optional REVERSE)
Documentation
Sort the range by a specified column.
With prefix, sorts in REVERSE order.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/ses.el.gz
(defun ses-sort-column (sorter &optional reverse)
"Sort the range by a specified column.
With prefix, sorts in REVERSE order."
(interactive "*sSort column: \nP")
(ses-check-curcell 'needrange)
(let ((min (ses-sym-rowcol (car ses--curcell)))
(max (ses-sym-rowcol (cdr ses--curcell))))
(let ((minrow (car min))
(mincol (cdr min))
(maxrow (car max))
(maxcol (cdr max))
keys extracts end)
(setq sorter (cdr (ses-sym-rowcol (intern (concat sorter "1")))))
(or (and sorter (>= sorter mincol) (<= sorter maxcol))
(error "Invalid sort column"))
;;Get key columns and sort them
(dotimes (x (- maxrow minrow -1))
(ses-goto-print (+ minrow x) sorter)
(setq end (next-single-property-change (point) 'cursor-intangible))
(push (cons (buffer-substring-no-properties (point) end)
(+ minrow x))
keys))
(setq keys (sort keys (lambda (x y) (string< (car x) (car y)))))
;;Extract the lines in reverse sorted order
(or reverse
(setq keys (nreverse keys)))
(dolist (x keys)
(ses-goto-print (cdr x) (1+ maxcol))
(setq end (point))
(ses-goto-print (cdr x) mincol)
(push (ses-copy-region (point) end) extracts))
(deactivate-mark)
;;Paste the lines sequentially
(dotimes (x (- maxrow minrow -1))
(ses-goto-print (+ minrow x) mincol)
(ses-set-curcell)
(ses-yank-cells (pop extracts) nil)))))