2

is there a way to sort only on second level headlines in org mode?

i am outlining a novel with top level headline being the chapter number and the second level headline being the timeline of the events in that chapter starting with a number = YYYYMMDD. the top level headline starts with a number representing the chapter. i have made the top level headline chapters high numbers in a hundreds sequence for ease of interpolating new chapters later, e.g., adding a chapter between 200 and the next chapter, 300, only requires adding at the end of the outline the new chapter with a top level headline of 250 and then resorting numerically. the second level headline is a different (chronological) number since chapter 300 may be a flashback occurring earlier in time than, say, chapter 100.

(see examples below)

if i could have two different ways of sorting (by chapter and by timeline), i could visualize the narrative more easily as i progress.

i have done some searching online for a solution and have considered tags and properties and functions but although i have been using emacs for about 5 years now (mainly as a text editor with .txt and .org and .markdown) formats, i am a writer with no programming or coding skills.

example of sorting by chapters (top level headline):

* 100 stranger comes to town
** 20190911
* 200 stranger meets chanteuse
** 20190925
* 300 stranger tells chanteuse a childhood story
** 19900525

example of sorting by timeline (second level headline):

* 300 stranger tells chanteuse a childhood story
** 19900525
* 100 stranger comes to town
** 20190911
* 200 stranger meets chanteuse
** 20190925

thanks for any help. i am not committed to org mode. any help will be appreciated. i could do this via a spreadsheet or a database manager, i realize, and have done so in the past, but would like to stay with emacs, which i use as aquamacs on an apple laptop.

  • Your aim is problematic. A top-level headline can have multiple second-level headlines. Let's say the first top-level headline 1 has one 2nd-level headline 1.1 and the second top-level headline 2 has two 2nd-level headlines 2.1 and 2.2. If 2.1 comes before and 2.2 after 1.1 the dates in the 2nd-level headlines do not define an order-relation on the top-level headlines. That prohibits sorting of the top-level headlines w.r.t. the 2nd-level headlines. – Tobias Sep 11 at 16:31
  • thanks for the comment. i think i am doing something differently than you are reading. sorry if it was not clear. first of all i am sorting on numbers i add to beginning of text of the headline so that i am sorting on my numbers, not org modes outline numbering. secondly, i plan on only having one second level headline. my sorting numerically works fine with examples i gave originally for top level (chapter) numbers. my problem is how to get org mode to sort by the first part of the second level headline (timeline) text, which is my chronological timeline number. thanks again. – rich ratzan Sep 11 at 16:54
  • I know that you do sort on your own criteria and not on org outline numbering. That gave me the possibility to address all headlines in short form by the org numbering independently of the order. I described in the text the order of the 2nd-level headlines as 2.1, 1.1, 2.2. (It does not matter that that order is induced by dates.) That order of 2nd-level headlines cannot define an order on the 1st level headlines. You build up your own convention about 2nd-level headlines (every 1st-level headline has only one 2nd-order one). That's problematic since you fight against the org structure. Don't! – Tobias Sep 11 at 17:18
  • thanks, tobias. i appreciate the time you are trying to help me. (this does not come easy to me.) what i think what you are saying (since i'll never have >1 second level headline, only one, i.e., 1.1, 2.1, 3.1, 4.1, et cet. and chronological prioriity would be between 2.1 - and, understandably, top level 2.0 - and 1.1 - and, therefore 1.0) is that there is no way to have org mode privilege second level headlines over top level in a sort function, regardless of org numbering or my textual numbering. thanks again. looks like spreadsheet time. – rich ratzan Sep 11 at 18:17
2

As we have already discussed in the comments second level headings are probably the wrong structural means for an alternative sorting of the first level headings. There could be two subheadings 2.1, 2.2 below the second top-level heading 2. If the natural order of the subheadings is 2.1, 1.1, 2.2 the subheadings do not define an order-relation on the top-level headings. Thereby, your personal convention of only using one subheading per heading does not really play a big role. The ordering of the top-level headings w.r.t. the subheadings does not fit Org's structure. Therefore, you will have no special support for that purpose.

Org can sort headlines with respect to timestamps. That should perfectly fit your purpose.

Consider the following org file:

* 1
CLOCK: [2019-09-11 Wed]
Body of the first heading.
* 2
CLOCK: [2019-08-01 Thu]
Body of the second heading.
* 3
CLOCK: [2019-07-01 Mon]
Body of the third heading.

Select the whole file with C-x h and call M-x org-sort with choice t (standing for [t]ime) the file is modified as follows:

* 3
CLOCK: [2019-07-01 Mon]
Body of the third heading.
* 2
CLOCK: [2019-08-01 Thu]
Body of the second heading.
* 1
CLOCK: [2019-09-11 Wed]
Body of the first heading.

Side note: Trailing empty lines belong to the previous section. So if you have empty lines at the end of the file those move together with heading 3. Avoid such newlines to get a uniform appearance of the file sorted w.r.t. time.

  • tobias - shall try your idea. i always assumed a timestamp was real time. once again i thank you a great deal for all the time and effort and shall get back to this exchange. – rich ratzan Sep 11 at 23:29
  • @richratzan When you type C-c ! you are offered the current date in the minibuffer but you can edit that date to your likings it can even be a time in the future. Eventually the timestamp is what you say it is. You can also make it more fine-grained and add hours and minutes. – Tobias Sep 12 at 0:50
  • tobias - works like a charm! thanks so much!! – rich ratzan Sep 13 at 3:00
  • @richratzan FYI: The official way to acknowledge the working answer is to accept it by clicking the checkmark V below the Downvote button. It may be that you didn't accept this answer on purpose since it actually does not answer the question how to sort w.r.t. the 2nd-level headline. Since THIS IS EMACS there is clearly a way of that sorting but it is more complicated. If you want I can also write up an answer for that actual question. From my perspective it does not serve your real task. – Tobias Sep 13 at 9:54
  • tobias - you guessed right: i did not know how to close discussion. shall try clicking on down arrow under 2. as far as i am concerned, your answer works perfectly. i could use a dedicated timeline app like aeon or cumbersome tinderbox’s timeline feature but i prefer to keep it simple. yours worked and is simple. i don’t need anymore assistance with this. thanks for all your help and patience! – rich ratzan Sep 14 at 14:05

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.