I use the property "pages" for the number of pages of an article or book.

There are two problems.


If I use C-c ^ to sort the items by the value of pages org-sort seems to interpret the numbers lexicographically: 1<100<20<3. Of course I need the natural order of numbers: 1<3<20<100.


If I use C-c a m to search for items with pages<20 I get the correct items and the "wrong" items: where the pages property is not defined. How to ignore the "wrong" items?


For the first problem, you have to use a function like this:

#+BEGIN_SRC emacs-lisp
(defun sort-by-pages ()
 (string-to-number (org-entry-get nil "pages")))

then type C-c ^ f and select the function sort-by-pages. The problem is properties in org-mode are strings, and you need this to convert them to numbers.

For problem two try: pages<20+pages<>"" the second one should eliminate the nulls.

  • Thanks, this solves both problems. But for the first one. Can I make the function more general?: With one argument, the name of the numerical property, for example "pages". (I have more than one numerical property. It's not good to define a new function for every new numerical property.) – rl1 Apr 23 '17 at 21:04
  • 1
    I don't think you can do that. I am pretty sure no arguments are allowed. – John Kitchin Apr 23 '17 at 22:06

I also used org properties for page numbers and have trouble sorting them numerically. I added your function, but when I use org-sort-entries, it first ask for Function for extracting keys:, which I select the sort-by-pages function. Then Emacs asks for Function for comparing keys (empty for default 'sort-subr' predicate):, which I don't know what to select here. Sorry if this is a dumb question as I don't know lisp yet. I've tried a couple functions based on intuition such as sort but get Wrong type argument: list-or-vector-p, 6

New contributor
tim is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Have you tried just to type ENTER after (empty for default 'sort-subr' predicate):? – rl1 Apr 8 at 17:01
  • yes, the output is Wrong number of arguments: (1 . 1), 2 – tim Apr 8 at 18:56
  • I don't know. I tested it again, it works. Maybe you should ask your question with details in a new thread. It's unlikely that much people read your question here. – rl1 Apr 8 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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