2

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
    *** 2
    *** 3
  ** C
    *** 1
    *** 2

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?

5
  • 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, 2018 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, 2018 at 7:13
  • 1
    The problem with your description is that the contents of sections are assigned to different sections after the "sort". But I gather from your comments in the answer that that's not what you wanted. You should fix the description of the problem to make clear that section B will still contain subsections 2 and 3 after the sort, not 1 and 2 as you have indicated (and ditto for C). IOW, the overall structure is preserved, it's just that at each level, headlines are sorted.
    – NickD
    Sep 17, 2021 at 21:00
  • 1
    @NickD Yes, indeed. I have corrected it. Thanks.
    – deshmukh
    Sep 18, 2021 at 11:46
  • Thank you! I realize it's a 3-year old question, but I think it's important to make them (both questions and answers) as accurate as possible, no matter how old they are. So, thanks for doing that!
    – NickD
    Sep 18, 2021 at 11:56

1 Answer 1

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.

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

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.