5

How can I sort habits in the org-agenda first by priority, then schedule + deadline + consistency?

I tried to set the org-agenda-sorting-strategy to various configurations, but ultimately habits are always sorted first by schedule + deadline + consistency followed by priority, the opposite of what I want.

I strongly prefer to avoid modifying the org source code like this SO question does.

1 Answer 1

4

Thanks to some guidance by Nicolas Goaziou, I created a solution that sorts the way I want by taking advantage of the user-defined sorting strategy:

(defun hw-org-agenda-sort-habits (a b)
  "Sort habits first by user priority, then by schedule+deadline+consistency."
  (let ((ha (get-text-property 1 'org-habit-p a))
        (hb (get-text-property 1 'org-habit-p b)))
    (when (and ha hb)
      (let ((pa (org-get-priority a))
            (pb (org-get-priority b)))
        (cond ((> pa pb) +1)
              ((< pa pb) -1)
              ((= pa pb) (org-cmp-values a b 'priority)))))))
(setq org-agenda-cmp-user-defined 'hw-org-agenda-sort-habits
      org-agenda-sorting-strategy '((agenda time-up user-defined-down habit-down)
                                    (todo priority-down category-keep)
                                    (tags priority-down category-keep)
                                    (search category-keep)))

Not the most elegant solution, but it does the job.

2
  • @holoncronweaver, might be nice to add the link to the org mailing list discussion with Nicolas you reference above. I'd like to check it out.
    – Joe
    Commented Apr 29, 2017 at 21:36
  • 1
    @Joe Done. His advice was simply to use org-agenda-cmp-user-defined, not much discussion beyond that I'm afraid. Commented Apr 29, 2017 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.