1

A natural approach to sorting an outline recursively would be the following:

(org-map-entries (lambda ()
                   (org-sort-entries nil ?f 'my/sort-function)
                   nil 'tree)

This doesn't work; it stops after sorting the top-level heading because org-sort-entries throws an error on any leaf node:

user-error: Nothing to sort

Do any alternatives exist?

2 Answers 2

1

I've convinced myself that my particular example should be treated as a bug since no explicitly recursive sorting mechanism appears to exist. In a pinch you can use the following:

(org-map-entries (lambda ()
                         (condition-case x
                             (org-sort-entries nil ?f 'my/sort-function)
                           (user-error)))
                       nil 'tree)

I'm going to submit it as a bug unless someone suggests a compelling explanation otherwise.

0

I came to this question looking for a simple way to sort all the headings in an Org buffer, recursively. Building on the answer by @ebpa, this is what I came up with:

(defun org-sort-buffer ()
  "Sort all entries in the current buffer, recursively."
  (interactive)
  (org-map-entries (lambda ()
                     (condition-case x
                         (org-sort-entries nil ?a)
                       (user-error)))))

Your Answer

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

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