Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you sort an Org clock table?

Tags:

emacs

org-mode

Here's an example Org clock table in Emacs:

#+BEGIN: clocktable :maxlevel 1 :scope file :tcolumns 1 :formula %
#+CAPTION: Clock summary at [2014-03-30 Sun 22:47]
| Headline     |   Time |     % |
|--------------+--------+-------|
| *Total time* | *4:31* | 100.0 |
|--------------+--------+-------|
| Item A       |   1:07 |  24.7 |
| Item B       |   1:06 |  24.4 |
| Item C       |   2:18 |  50.9 |
#+TBLFM: $3='(org-clock-time% @2$2 $2..$2);%.1f
#+END:

I want to sort the table by the % column. Is this possible?

Here's a more complex example:

#+BEGIN: clocktable :maxlevel 2 :scope file :tcolumns 1 :formula %
#+CAPTION: Clock summary at [2014-03-30 Sun 22:48]
| Headline           |   Time |     % |
|--------------------+--------+-------|
| *Total time*       | *4:31* | 100.0 |
|--------------------+--------+-------|
| Item A             |   1:07 |  24.7 |
| \__ Item A1        |   0:07 |   2.6 |
| \__ Item A2        |   1:00 |  22.1 |
| Item B             |   1:06 |  24.4 |
| \__ Item B1        |   1:06 |  24.4 |
| Item C             |   2:18 |  50.9 |
| \__ Item C1        |   2:18 |  50.9 |
#+TBLFM: $3='(org-clock-time% @2$2 $2..$2);%.1f
#+END:

In this instance, the top level items should be sorted by their % values, but within each subtree, the secondary level items should also be sorted by their % values.

like image 731
Naomi Slater Avatar asked Mar 30 '14 20:03

Naomi Slater


2 Answers

The time clocking code for Org-mode org-clock.el doesn't support any sorting facilities. The function org-clocktable-write-default creates tables by very rigid algorithm with limited variants of tuning, all of them are in the variable org-clocktable-defaults. Thus, dynamic block clocktable generates org tables "as is", http://orgmode.org/manual/The-clock-table.html contains exhaustive list of settings.

You can sort first table manually by placing the cursor in the 3 column and executing M-x org-table-sort-lines [n]umeric.

As to sorting second table with "knowledge about structure of levels" - it seems to be impossible without profound changes in the function org-clocktable-write-default.

like image 154
artscan Avatar answered Oct 16 '22 14:10

artscan


The built-in formatter org-clocktable-write-default sorts the entries just row by row with no idea of levels. I was looking for the same functionality and ended up with writing my own formatter (not a formatter really) that sorts the entries with respect to levels. It has a limitation though that it can only sort on name or time, since the result of sorting on time is essentially the same as sorting on percentage, this's acceptable in your case.

like image 22
louiet Avatar answered Oct 16 '22 16:10

louiet