Function: sort-numeric-fields

sort-numeric-fields is an autoloaded, interactive and byte-compiled function defined in sort.el.gz.

Signature

(sort-numeric-fields FIELD BEG END)

Documentation

Sort lines in region numerically by the ARGth field of each line.

Fields are separated by whitespace and numbered from 1 up. Specified field must contain a number in each line of the region, which may begin with "0x" or "0" for hexadecimal and octal values. Otherwise, the number is interpreted according to sort-numeric-base. With a negative arg, sorts by the ARGth field counted from the right. Called from a program, there are three arguments: FIELD, BEG and END. BEG and END specify region to sort.

Probably introduced at or before Emacs version 18.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/sort.el.gz
;;;###autoload(put 'sort-numeric-base 'safe-local-variable 'integerp)

;;;###autoload
(defun sort-numeric-fields (field beg end)
  "Sort lines in region numerically by the ARGth field of each line.
Fields are separated by whitespace and numbered from 1 up.
Specified field must contain a number in each line of the region,
which may begin with \"0x\" or \"0\" for hexadecimal and octal values.
Otherwise, the number is interpreted according to sort-numeric-base.
With a negative arg, sorts by the ARGth field counted from the right.
Called from a program, there are three arguments:
FIELD, BEG and END.  BEG and END specify region to sort."
  (interactive "p\nr")
  (let ;; To make `end-of-line' and etc. to ignore fields.
      ((inhibit-field-text-motion t))
    (sort-fields-1 field beg end
		   (lambda ()
		     (sort-skip-fields field)
		     (let* ((case-fold-search t)
			    (base
			     (if (looking-at "\\(0x\\)[0-9a-f]\\|\\(0\\)[0-7]")
				 (cond ((match-beginning 1)
					(goto-char (match-end 1))
					16)
				       ((match-beginning 2)
					(goto-char (match-end 2))
					8)
				       (t nil)))))
		       (string-to-number (buffer-substring (point)
							   (save-excursion
							     (forward-sexp 1)
							     (point)))
					 (or base sort-numeric-base))))
		   nil)))