1

Running Emacs 25.2.2.

Consider the following heading hierarchy:

* L1
  ** C
    *** 2
    *** 1
  ** B
    *** 3
    *** 2

I want to sort it to:

* L1
  ** B
    *** 1
    *** 2
  ** C
    *** 2
    *** 3

org-sort lets me sort entries under a heading at a time. But it does not let me sort the entire tree like the case above.

How do I get this done?

  • If you construct the tree more or less randomly, maybe this would make sense, but if you construct it (as I think most people do) with malice aforethought, then why would you want to scramble the structure by sorting? What is the use case? – NickD Jul 23 '18 at 17:01
  • @NickD Thanks. This is a shopping list. So, L1 is 'Shopping List'. L2 contains headings such as '1Grocer', '2Hardware', etc. That 1, 2 ensure that these headings always get sorted the way I expect. And frankly, they do not move much. But under '1Grocer', I have Bread and Rice. Now, if I refile Eggs from elsewhere, it will either go the top or bottom. That will need sorting. As an aside, I think if there is a reason to have org-sort there is a reason to have org-sort-recursive :) – deshmukh Jul 24 '18 at 7:13
2

How about something like this:

(defun sort-all-org-entries () (interactive) (let ((fun #'(lambda nil (condition-case nil (org-sort-entries nil ?a) (user-error t))))) (org-map-entries fun)))

The condition-case stuff is just because org-sort-entries irritatingly signals an error on the deepest levels rather than just skipping them.

Actually, I've just noticed that this doesn't quite do what you want, since it only sorts within each entry, and you want to sort between entries.

  • Thanks. As you said, I want to sort within and between entries --- something like recursive sort. – deshmukh Jul 23 '18 at 14:12
  • Actually, it does what I wanted. Thanks a ton! – deshmukh Jul 26 '18 at 13:37

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.