In an Org-mode document, let's imagine that my entries are following this pattern:

* TODO Note page 2 [0/1]
  my notes, must be at second position after sorting
  - [ ] todo 1

* Note page 14                                                        :A_Tag:
  my notes, must be at third position after sorting

* Note page 1
  my notes, must be at first position after sorting

Question: how to sort these entries according to page number?

Expected result is:

* Note page 1
  my notes, must be at first position after sorting 

* TODO Note page 2 [0/1]
  my notes, must be at second position after sorting
  - [ ] todo 1

* Note page 14                                                        :A_Tag:
  my notes, must be at third position after sorting

I know the function org-sort-entries but AFAIK we can not use it with default options. We must define that the sort must be performed according to a pattern like

^* <ignore TODO> Note page [Number] <ignore tag list>$

For instance, if I use org-sort-entries + [a] I get:

* Note page 1
  my notes, must be at first position after sorting

* Note page 14                                                        :A_Tag:
  my notes, must be at second position after sorting

* TODO Note page 2 [0/1]
  my notes, must be at third position after sorting
  - [ ] todo 1

I get an alphabetically sorted output, however, this is not what I want to do. What is the right way to do that?

----- update

with your function org-page-sort I get:

* Note page 1
  my notes, must be at first position after sorting* TODO Note page 2 [0/1]
  my notes, must be at second position after sorting
  - [ ] todo 1

* Note page 14                                                        :A_Tag:
  my notes, must be at third position after sorting

maybe a little bug. However, I do not see where is the problem as your function prints

looking at page Note page 2 [0/1]
looking at page Note page 14
looking at page Note page 1

as expected.

Anyway thank you very much (I upvoted your answer), this is the kind of solution I was looking for!

  • 1
    I just tried org-sort (which ends up calling org-sort-entries) on your example, and it sorts them correctly. This is with org-mode 9.1.14 – rpluim Sep 14 at 11:58
  • @rpluim Thanks for the feedback. Actually, I'm under Org-9.1.1 and I was not aware of this function. I would be happy with a solution working with Org-9.1.1. Otherwise, I will upgrade and try your suggestion – Picaud Vincent Sep 14 at 12:38
  • @rpluim sorry I messed up my example... now it is fixed, page 20 is replaced by page 2... If you try again, at least with my Org version, it does not work anymore. – Picaud Vincent Sep 14 at 12:51
  • I don't think the version of org matters too much here. One hacky solution is to write your page numbers with 2 digits always. – rpluim Sep 14 at 12:59
  • @rpluim I understand your point, but I would like a nicer solution: said in another way, I do not want to write "... page 002" :-) – Picaud Vincent Sep 14 at 13:02
up vote 3 down vote accepted

You need to define a custom function to get the key for sorting. Then you can tell org-sort-entries to use the function. Below I use a regex to get the page number and convert it to a number (it starts out as a string).

(defun org-page-sort ()
  (interactive)
  (mark-whole-buffer)
  (org-sort-entries
   nil ?f
   (lambda ()
     (let ((heading (nth 4 (org-heading-components))))
       (if (string-match "page \\([0-9]+\\)" heading)
           (string-to-number (match-string 1 heading))
         1)))))
  • I just wrote the same operation, but with more lines of code :-) – rpluim Sep 14 at 13:26
  • Thank you John, this is the kind of approach I wanted. However, maybe a little bug (see my update). Anyway I upvoted :) – Picaud Vincent Sep 14 at 13:29
  • 1
    huh. Weird, it worked when I ran it, but after you saw that error, so did I, but I also saw that with plain org-sort-entries. I edited the function a little, and it works like it did before now, after restarting emacs. weird. – John Kitchin Sep 14 at 14:01
  • 1
    BTW, in emacs-26, string-to-int is now written string-to-number (and the alias between the two has been removed) – rpluim Sep 14 at 14:22

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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