Function: tabulated-list-sort

tabulated-list-sort is an interactive and byte-compiled function defined in tabulated-list.el.gz.

Signature

(tabulated-list-sort &optional N)

Documentation

Sort Tabulated List entries by the column at point.

With a numeric prefix argument N, sort the Nth column.

If the numeric prefix is -1, restore order the list was originally displayed in.

View in manual

Probably introduced at or before Emacs version 24.3.

Key Bindings

Aliases

Buffer-menu-sort (obsolete since 28.1)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/tabulated-list.el.gz
(defun tabulated-list-sort (&optional n)
  "Sort Tabulated List entries by the column at point.
With a numeric prefix argument N, sort the Nth column.

If the numeric prefix is -1, restore order the list was
originally displayed in."
  (interactive "P")
  (when (and n
             (or (>= n (length tabulated-list-format))
                 (< n -1)))
    (user-error "Invalid column number"))
  (if (equal n -1)
      ;; Restore original order.
      (progn
        (unless tabulated-list--original-order
          (error "Order is already in original order"))
        (setq tabulated-list-entries
              (sort tabulated-list-entries
                    (lambda (e1 e2)
                      (< (gethash e1 tabulated-list--original-order)
                         (gethash e2 tabulated-list--original-order)))))
        (setq tabulated-list-sort-key nil)
        (tabulated-list-init-header)
        (tabulated-list-print t))
    ;; Sort based on a column name.
    (let ((name (if n
		    (car (aref tabulated-list-format n))
		  (get-text-property (point)
				     'tabulated-list-column-name))))
      (if (nth 2 (assoc name (append tabulated-list-format nil)))
          (tabulated-list--sort-by-column-name name)
        (user-error "Cannot sort by %s" name)))))