Function: csv-sort-numeric-fields

csv-sort-numeric-fields is an interactive and natively compiled function defined in csv-mode.el.

Signature

(csv-sort-numeric-fields FIELD BEG END)

Documentation

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

If not set, the region defaults to the CSV records around point. Fields are separated by csv-separators. Null fields are allowed anywhere and sort as zeros. Field indices increase from 1 on the left or decrease from -1 on the right. A prefix argument specifies a single field, otherwise prompt for field index. Specified non-null 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. Ignore blank and comment lines. When called non-interactively, FIELD is a single field index; BEG and END specify the region to sort.

Key Bindings

csv-mode-map C-c C-n

Source Code

;; Defined in /nix/store/3gmjnyw9n226b7k7jamikx980vf8267p-emacs-packages-deps/share/emacs/site-lisp/elpa/csv-mode-1.27/csv-mode.el
(defun csv-sort-numeric-fields (field beg end)
  "Sort lines in region numerically by the ARGth field of each line.
If not set, the region defaults to the CSV records around point.
Fields are separated by `csv-separators'.
Null fields are allowed anywhere and sort as zeros.
Field indices increase from 1 on the left or decrease from -1 on the right.
A prefix argument specifies a single field, otherwise prompt for field index.
Specified non-null 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.
Ignore blank and comment lines.
When called non-interactively, FIELD is a single field index;
BEG and END specify the region to sort."
  ;; (interactive "*P\nr")
  (interactive (csv-interactive-args 'single))
  (barf-if-buffer-read-only)
  (csv-sort-fields-1 field beg end
		 (lambda ()
		   (csv-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))