emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Orgmode] Useful utility function: org-sort-multi


From: Ryan C. Thompson
Subject: [Orgmode] Useful utility function: org-sort-multi
Date: Fri, 28 Aug 2009 18:38:00 -0700
User-agent: Thunderbird 2.0.0.23 (X11/20090817)

I found myself having to sort by multiple criteria, and I was doing it with multiple calls to org-sort-entries-or-items. Then I decided to abstract the repetition into a function. Here it is:

(defun org-sort-multi (&rest sort-types)
"Sort successively by a list of criteria, in descending order of importance. For example, sort first by TODO status, then by priority, then by date, then alphabetically, case-sensitive. Each criterion 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''.
So, the example above could be accomplished with:
 (org-sort-multi ?o ?p ?t (t . ?a))"
  (interactive)
  (mapc #'(lambda (sort-type)
(when (characterp sort-type) (setq sort-type (cons nil sort-type)))
            (org-sort-entries-or-items (car sort-type) (cdr sort-type)))
        (reverse sort-types)))

Note the call to reverse. This makes it so that the first criterion you provide is the dominant criterion. Try it out to see how it works, and let me know if there's a better way to pass the arguments.

Just as an example, the particular sorting function I wanted to write now becomes this:

(defun org-sort-custom ()
"Sort children of node by todo status and by priority and by date, so the * TODO [#A] items with latest dates go to the top."
  (interactive)
  (org-sort-multi ?o ?p ?T))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]