14

I'm taking notes in a largish org-mode file that has the following structure:

* Toto, A Notes about Toto. * Titi, C Notes about Titi. * Tutu, A Notes about Tutu. 

I'd like to sort the file so that all the headings with A precede the headings with B, which in turn precede the headings with C, and so on. Is there an automated way to do that with org-mode?

2
  • 4
    Did you try org-sort? Commented Jan 19, 2015 at 15:45
  • C-c ^ from memory (could be something similar ending in ^, like C-x C-c ^ etc.) Commented Jan 19, 2015 at 15:51

2 Answers 2

19

There is an org-sort command but you may need to tweak your file structure to get the results you want (if I am understanding the question properly).

Use org-sort (C-c ^) to sort entries in the outline, rows in a table, or items in a list. For example from a heading you can call org-sort to sort all the child entries.

In your example you want to sort the top-level entries in your outline, so select all the entries first. When you call org-sort you'll be prompted for how to sort: alphabetically, by priority, and so on.

I'm guessing from your example that the end result you expect is:

* Toto, A Notes about Toto. * Tutu, A Notes about Tutu. * Titi, C Notes about Titi. 

That is, you are looking to sort alphabetically but have that trailing letter take precedence? There is no built-in sort for that. If you changed the entries to put that sort letter first (e.g. A, Toto) then a normal alphabetic sort would work. Another option would be to move that information to a property on each item, then you can sort by property.

As @nanny has suggested in the comments, org-sort can also call a custom sort function. That way you can use your existing file without having to change all the entries. Here's a sort function that finds the current headline text and appends the last character to the beginning so that it takes precedence. You'll need something more robust if your headlines don't all follow that exact pattern.

(defun my-sort-last-letter () (let ((text (plist-get (cadr (org-element-headline-parser (line-end-position))) :raw-value))) (concat (string (aref text (- (string-width text) 1))) text))) 
4
  • 1
    An alternative to changing the file is using the [f]unc sorting type in org-sort and writing a function to sort based on the existing structure. Commented Jan 19, 2015 at 16:25
  • That solved my problem quite nicely, after a quick pass of query-replace-regexp to edit the few headings that didn't strictly follow the right format. Much easier than converting all of my data to properties. Commented Jan 20, 2015 at 3:10
  • ...or you could move it to a tag, i.e. "* Toto :A:" then sort C-c ^ r TAGS Commented Jan 23, 2015 at 7:15
  • 1
    Just to let you know that I have this problem exactly once a year, and I've used your solution again yesterday. Commented Dec 22, 2016 at 15:16
0

I'm putting headlines with different priorities anywhere in a file, I'm not concerned about the order. Later I'm viewing them sorted with agenda views. By using tags, sorted headlines from concrete categories are viewed with custom agenda views.

I know that's not the answer for how to sort headlines in a file, but viewing headlines with agenda views is always a best way. You can insert prioritized headlines quickly, without worrying about the order.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.