1

If I have a list of books' call numbers in Dewey Decimal in a buffer, is this something I could sort with Emacs regex -- or is this an elisp task? For example, 261.1 C87 comes before 261.104 Z99 because the first one can be understood as 261.100 C87. So each column/place must be compared. And then of course the alphabet parts, which means 261.1 C87 comes before 261.1 Ch2. I'm just wondering if there are any Emacs regex experts who would see a good way. Otherwise, I'll just do elisp on it.

1 Answer 1

2

Use command sort-numeric-fields.

C-h f sort-numeric-fields tells you:

sort-numeric-fields is an interactive autoloaded Lisp function in sort.el.

It is bound to menu-bar edit sort sort-numeric-fields.

(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.

See also the Emacs manual, node Sorting. There you see, in addition to a general explanation of sorting and descriptions of other sorting commands:

M-x sort-fields

Divide the region into lines, and sort by comparing the contents of one field in each line. Fields are defined as separated by whitespace, so the first run of consecutive non-whitespace characters in a line constitutes field 1, the second such run constitutes field 2, etc.

Specify which field to sort by with a numeric argument: 1 to sort by field 1, etc. A negative argument means count fields from the right instead of from the left; thus, minus 1 means sort by the last field. If several lines have identical contents in the field being sorted, they keep the same relative order that they had in the original buffer.

M-x sort-numeric-fields

Like M-x sort-fields except the specified field is converted to an integer for each line, and the numbers are compared. 10 comes before 2 when considered as text, but after it when considered as a number. By default, numbers are interpreted according to sort-numeric-base, but numbers beginning with 0x or 0 are interpreted as hexadecimal and octal, respectively.

3
  • Thanks for the good info, Drew. Actually, a M-x sort-lines did the trick. Apparently, I was in over-kill mode.
    – 147pm
    Commented Aug 24, 2016 at 19:39
  • Is it possible to specify field separator such as , instead of space?
    – Arktik
    Commented Oct 11 at 18:02
  • @Arktik: The manual says fields are searated by whitespace.
    – Drew
    Commented Oct 12 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.