1

The comparison function below meant to compare headings using custom CREATED property which store org timestamp. The problem is, it doesn't work - the sorting fallback to next sorting strategy which is alpha-up.

(defun jarfar/org-agenda-cmp-user-defined-created-date (a b)
  "Org Agenda user function to sort tasks based on CREATED property."
  (let* (
          (marker-a (get-text-property 0 'org-marker a))
          (marker-b (get-text-property 0 'org-marker b))
          (time-a (if marker-a (org-entry-get marker-a "CREATED") nil))
          (time-b (if marker-b (org-entry-get marker-b "CREATED") nil))
          (time-a (if (jarfar/is-org-timestamp time-a) (org-time-string-to-time time-a) nil))
          (time-b (if (jarfar/is-org-timestamp time-b) (org-time-string-to-time time-b) nil)))

    (if (and time-a time-b)
      (if (org-time< time-a time-b)
        -1
        (if (org-time> time-a time-b) 1 nil))
      (if time-a -1 1)
      )))

(defun jarfar/is-org-timestamp (arg)
  "Returns 't' if 'arg' is org timestamp string, otherwise returns nil."
  (and arg (stringp arg) (string-match org-ts-regexp0 arg)))


Example data file:

* Tasks
** TODO Task 1
:PROPERTIES:
:CREATED: [2020-03-01 Sun]
:END:

** TODO Task 2
:PROPERTIES:
:CREATED: [2020-05-01 Fri]
:END:

** TODO Task 3
:PROPERTIES:
:CREATED: [2020-07-01 Wed]
:END:

** TODO Task 4
:PROPERTIES:
:CREATED: [2020-06-01 Mon]
:END:

Any idea what is wrong with the function?

  • How does it fail? – NickD Sep 11 at 16:23
  • I updated the post with error for every fail case. – Adam Faryna Sep 11 at 17:04
  • Can you specify which functions are giving those errors in each case? M-x toggle-debug-on-error will enable a backtrace when the error triggers – erikstokes Sep 11 at 18:58
  • I fixed some bugs and redefined the problem. – Adam Faryna Sep 11 at 19:54
0

I managed to figure out what was the problem, I simply over complicate it. org-time comparison functions work with output of org-entry-get not org-time-string-to-time.

(defun jarfar/org-agenda-cmp-user-defined-created-date (a b)
  "Org Agenda user function to sort tasks based on CREATED property."
  (let* (
          (marker-a (get-text-property 0 'org-marker a))
          (marker-b (get-text-property 0 'org-marker b))
          (time-a (if marker-a (org-entry-get marker-a "CREATED") nil))
          (time-b (if marker-b (org-entry-get marker-b "CREATED") nil)))

    (if (and time-a time-b)
      (if (org-time< time-a time-b)
        -1
        (if (org-time> time-a time-b) 1 nil))
      (if time-a -1 1)
      )))
| improve this answer | |

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.