5

How can I randomize headers in org mode?

e.g. If I had a list like this, :

* apple
  1
* lemon
  2
* banana
* orange

How can I randomize those headers and get lists like these?:

* lemon
  2
* apple
  1
* orange
* banana

or

* orange
* lemon
  2
* banana
* apple
  1

(I'm planning to randomize a much longer list.)

Drew
  • 75,699
  • 9
  • 109
  • 225
stacko
  • 1,577
  • 1
  • 11
  • 18

3 Answers3

4

I found the answer here. All I had to do was creating a function and call it once I got into org-sort f(function).

stacko
  • 1,577
  • 1
  • 11
  • 18
  • Using random as a sorting predicate [is a wrong algorithm](http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html). – Juancho Apr 17 '16 at 19:19
  • 3
    To better help others, please post some code for your solution. The link is fine, but you could help a reader here a bit more. – Drew Apr 17 '16 at 20:54
  • I get the following message:"Function for comparing keys (empty for default `sort-subr` predicate) and a list of option. Just pressing enter is not working. – falematte Jul 28 '17 at 16:16
  • When I call org-sort Emacs ask me two functions respectively for extracting and comparing. Which one do I have to choose? – falematte Aug 11 '17 at 14:48
1

In emacs 28 it seems you need to enter two functions. The first one is used to extract keys from entries. Theses keys will be assigned to each entry. Then you need to specify a function to compare the keys in order to do the sorting.

So the key-extraction function must just generate random numbers. You can just use the built-in random function. The function for comparing keys can then just be <.

1

I had to combine the other two answers here to get this to work, so here's a complete answer with explanation and code from the linked 2011 email.

We will use org-sort (C-c ^), which can use an arbitrary function for its sort algorithm. In emacs 28 and later, we also have to supply a function to use to compare the outputs of the sort algorithm. We will use the less-than function, <.

  1. Define a sort function
(defun my-org-random-sort ()
  (random 1000))

It probably matters what number you put, but the email thread suggested 1000 and I haven't tried anything else.

  1. At the subtree/table/list you want to randomize, type:

C-c ^ f my-org-random-sort <

Niles
  • 111
  • 4