Emacs: Sort File by Time, Size, in Dired

By Xah Lee. Date: . Last updated: .

Here's a emacs command that lets you sort dired file listing by any {date/time, size, name, dir}.

(defun xah-dired-sort ()
  "Sort dired dir listing in different ways.
Prompt for a choice.
URL `http://ergoemacs.org/emacs/dired_sort.html'
Version 2015-07-30"
  (let (-sort-by -arg)
    (setq -sort-by (ido-completing-read "Sort by:" '( "date" "size" "name" "dir")))
     ((equal -sort-by "name") (setq -arg "-Al --si --time-style long-iso "))
     ((equal -sort-by "date") (setq -arg "-Al --si --time-style long-iso -t"))
     ((equal -sort-by "size") (setq -arg "-Al --si --time-style long-iso -S"))
     ((equal -sort-by "dir") (setq -arg "-Al --si --time-style long-iso --group-directories-first"))
     (t (error "logic error 09535" )))
    (dired-sort-other -arg )))

Note: this may not work in Mac OS X. You need to have GNU's version of ls.

You can give it a keybinding for easy call.

(require 'dired )
(define-key dired-mode-map (kbd "s") 'xah-dired-sort)

Dired topics

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs