5

I have noticed that when I look at the TODO list, I usually only get things done from the top half of the portion since I read from top to bottom, and by the time I get to half way to the bottom, I find a TODO that could be done. So I was wondering, is there a way to mix up the TODO list so that the ordering is randomized?

THIS USER NEEDS HELP
  • 3,136
  • 4
  • 30
  • 55
  • 1
    Why not play with `org-sort-entries` and sort it a different way each time depending upon your mood? `M-x describe-function RET org-sort-entries RET` – lawlist Apr 22 '16 at 00:59
  • @lawlist I do use "contexts" from GTD to display different agendas for different situations, but I have one main agenda that I use the most frequently regardless of the contexts, and it is that list that I wanted to randomize. Also, I don't give many meta attributes to TODO entries, so many options from `org-sor-entries` don't really work for me. – THIS USER NEEDS HELP Apr 22 '16 at 06:31
  • At the bottom of the doc-string it talks about using the options for `?f` and `?F` -- i.e., you can write your own sorting functions (e.g., randomize) and incorporate that into the functionality of `org-sort-entries`. Perhaps someone will write it up if you need some help, and you can always offer a bounty in a few days if no one has responded. – lawlist Apr 22 '16 at 06:54
  • @lawlist Ah thank you. Unfortunately, my lisp skill lacks depth and would need some help to get there – THIS USER NEEDS HELP Apr 22 '16 at 07:01

2 Answers2

2

Org mode lets you sort by a key in the PROPERTIES drawer, so one way to do this is to set a random sorting key in there at the start of the search. The following code does just that. After the sort is done it removes the sort keys and deletes empty property drawers, leaving things just as they were.

(defun org-sort-entries-randomly()
  (interactive)
  (if (org-at-heading-p)
      (progn
       (setq start (point))
       (org-end-of-subtree)
       (while (> (point) start)
         (if (org-at-heading-p)
             (org-set-property "sort-key" (format "%d" (+ 10000 (random 10000)))))
         (previous-line))
       (org-sort-entries nil ?r nil nil "sort-key")
       (goto-char start)
       (org-end-of-subtree)
       (while (> (point) start)
         (if (org-at-heading-p)
             (org-delete-property "sort-key" "PROPERTIES"))
         (previous-line)))
    (message "Not at heading")))
justinhj
  • 11,147
  • 11
  • 58
  • 104
2

As mentioned org-sort let's you specify a function to sort by:

If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be called with point at the beginning of the record. It must return either a string or a number that should serve as the sorting key for that record.

As it happens random is a function that returns a random number. So M-x org-sort f random will randomize the sort order in the org file.

However instead of changing the file you could use org-agenda to view the todos in a randomized order. By setting org-agenda-cmp-user-defined you can customize the sort order. This function will take two arguments (the agenda entries to compare) and should return -1,1 or 0 depending on which of the entries is "smaller". Here is such a function:

(defun org-random-cmp (a b)
  "Return -1,0 or 1 randomly"
  (- (mod (random) 3) 1))

And here is an agenda view that shows all TODO items in a random order:

(add-to-list 'org-agenda-custom-commands
  '("r" "All todo items in a random order"
    alltodo ""
    ((org-agenda-cmp-user-defined #'org-random-cmp))))
erikstokes
  • 1,197
  • 6
  • 15
  • In order to sort correctly the sort function should return the same result when comparing two entities. If you randomize it the sort may never end. – justinhj Apr 25 '16 at 16:29
  • Hm. Could you confirm that this works? It doesn't seem to change any ordering on mine. `org-random-cmp` function works as intended though. Also, is this supposed to randomize the order each time you open org-agenda? – THIS USER NEEDS HELP May 04 '16 at 22:23
  • Do you mean the `org-sort` function or the agenda view? Both worksfor me. The agenda view is meant to randomize each time you open the agenda. – erikstokes May 05 '16 at 00:09
  • @justinhj That's true, but it's unlikely for the sort to never terminate. I pretty sure it terminates with probability 1. It is likely to take longer than a normal sort, but no enough to matter. – erikstokes May 05 '16 at 00:10