0

My org file structure is this:

* Heading 1
:PROPERTIES:
:EVENT_CREATED: [2016-11-28]
:END:
* Heading 2
:PROPERTIES:
:EVENT_CREATED: [2016-10-27]
:END:
* Heading 3
:PROPERTIES:
:EVENT_CREATED: [2019-02-08]
:END:

I essentially want to sort each all headings by the EVENT_CREATED property, so that they are on chronological order.

2
  • Two things: 1) your example is badly formed, since the property drawers are syntactically illegal - each EVNT_CREATED should be inside a drawer, demarcated by :PROPERTY: above and :END: below it. 2) It will make your life easier if the times are Org mode timestamps (probably inactive ones): [2016-11-27]. Finally a hint: org-sort-entries is the function you are looking for, but it will require that you write a custom get-key function (and maybe a custom comparison function to compare the keys) - do C-h f org-sort-entries to see its doc string for the details (which are numerous). – NickD Nov 29 '20 at 3:21
  • I took the liberty of fixing the property drawers and changing the property values to Org mode inactive timestamps, as described in my previous comment (I also changed the name of the property to EVENT_CREATED but that's not particularly important, although I find it more readable). Let me know if you object. – NickD Nov 29 '20 at 3:35
1

Here's some code to do the sorting - it assumes the corrected version of your example, that you are using inactive timestamps for the values of the EVENT_CREATED property for each header and that every top-level node contains an EVENT_CREATED property:

(defun ndk/get-key ()
  (org-time-string-to-seconds (substring  (org-entry-get (point) "EVENT_CREATED") 1 -1)))

(defun ndk/cmp (t1 t2)
  (< t1 t2))

(defun ndk/org-sort-entries ()
  (interactive)
  (org-sort-entries nil ?f #'ndk/get-key #'ndk/cmp))

You can add these function definitions to your init file, or load them from a file or evaluate them in your *scratch* buffer.

To use them, select the region that you want to sort in the usual way and say M-x ndk/org-sort-entries.

This sorts in ascending order. If you want descending order, change the ?f to ?F in the definition of nkd/org-sort-entries. For more details, see the doc string for org-sort-entries with C-h f org-sort-entries RET.

Some notes:

  • the ndk/get-key function is called at the beginning of each entry. It retrieves the value of the property, strips the initial and final square brackets and converts the string to a UNIX time in seconds (since the "epoch", 1970-01-01 00:00:00).

  • the convention of using inactive timestamps for the values of the EVENT_CREATED property allows the use of the already existing Org mode function org-time-string-to-seconds to convert to a float that can be easily used in comparisons when sorting.

  • the convention also allows easy entry of the property value: C-c C-x p asks you for the property name (and probably already has EVENT_CREATED filled in, so all you have to do is press RET here) and then asks you for the property value, which you can enter with C-c ! RET to give it an inactive timestamp with the current date. This has nothing to do with the sorting, but it is one reason that I recommended you use inactive timestamps as values in the first place.

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.