10

When Dired sorts files by name, it sorts in alphabetical order. That's fine with letters; however, when the file names start with numbers, it would be better to sort in numerical value.

Example:

This is how dired sorts my files:

10 11 12 13 ... 1 21 22 23 ... 2 31

I want 1 2 3 4 … 9 10 11 …

11

Here's my config:

(setq dired-listing-switches "-laGh1v --group-directories-first")

The relevant part is -1v.

5

Besides @abo-abo answer, I just want to quote the documentation:

dired-listing-switches is a variable defined in `dired.el'. Its value is "-Al --si --time-style long-iso"

Documentation: Switches passed to ls for Dired. MUST contain the l option. May contain all other options that don't contradict -l; may contain even F, b, i and s. See also the variable dired-ls-F-marks-symlinks concerning the F switch. On systems such as MS-DOS and MS-Windows, which use ls emulation in Lisp, some of the ls switches are not supported; see the doc string of insert-directory in ls-lisp.el for more details.

Basically, you can customize the switches you want dired to use when calling ls

2

The given answers are better for this precise problem because they hook directly into the behavior of Dired. However for generality's sake I want to mention the sort-numeric-fields command, which is specifically for sorting numbers by magnitude rather than lexicographically.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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