function to org-sort by three (3) criteria: due date / priority / title

Posted by lawlist on Super User See other posts from Super User or by lawlist
Published on 2013-06-24T19:44:01Z Indexed on 2013/06/29 22:23 UTC
Read the original article Hit count: 198

Filed under:
|

Is anyone aware of an org-sort function / modification that can refile / organize a group of TODO so that it sorts them by three (3) criteria: first sort by due date, second sort by priority, and third sort by by title of the task?


EDIT: If anyone can please help me to modify this so that undated TODO are sorted last, that would be greatly appreciated -- at the present time, undated TODO are not being sorted:

;; multiple sort
(defun org-sort-multi (&rest sort-types)
  "Multiple sorts on a certain level of an outline tree, or plain list items.

SORT-TYPES is a list where each entry is either a character or a
cons pair (BOOL . CHAR), where BOOL is whether or not to sort
case-sensitively, and CHAR is one of the characters defined in
`org-sort-entries-or-items'.  Entries are applied in back to
front order.

Example:  To sort first by TODO status, then by priority, then by
date, then alphabetically (case-sensitive) use the following
call:

  (org-sort-multi '(?d ?p ?t (t . ?a)))"

(interactive)
  (dolist (x (nreverse sort-types))
    (when (char-valid-p x)
      (setq x (cons nil x)))
    (condition-case nil
        (org-sort-entries (car x) (cdr x))
      (error nil))))


;; sort current level
(defun lawlist-sort (&rest sort-types)
  "Sort the current org level.

SORT-TYPES is a list where each entry is either a character or a
cons pair (BOOL . CHAR), where BOOL is whether or not to sort
case-sensitively, and CHAR is one of the characters defined in
`org-sort-entries-or-items'.  Entries are applied in back to
front order.

Defaults to \"?o ?p\" which is sorted by TODO status, then by
priority"
  (interactive)
  (when (equal mode-name "Org")
    (let ((sort-types (or sort-types
                          (if (or (org-entry-get nil "TODO")
                                  (org-entry-get nil "PRIORITY"))
                              '(?d ?t ?p) ;; date, time, priority
                            '((nil . ?a))))))
      (save-excursion
        (outline-up-heading 1)
        (let ((start (point))
              end)
          (while (and (not (bobp)) (not (eobp)) (<= (point) start))
            (condition-case nil
                (outline-forward-same-level 1)
              (error (outline-up-heading 1))))
          (unless (> (point) start)
            (goto-char (point-max)))
          (setq end (point))
          (goto-char start)
          (apply 'org-sort-multi sort-types)
          (goto-char end)
          (when (eobp)
            (forward-line -1))
          (when (looking-at "^\\s-*$")
;;            (delete-line)
)
          (goto-char start)
;;          (dotimes (x ) (org-cycle))
          )))))

© Super User or respective owner

Related posts about emacs

Related posts about org-mode