I'm using windows 10 and emacs 26.1.

In org mode if I want to sort by one column I use

org-sort

It's sort by text, time, function.

Nice.

But what about sorting by two or more columns?

  • 1
    org-table-sort-lines generates sort keys from a single given column, so no support for multiple column sorting. You'll need to ad a new column that combines other columns into a valid key for sorting. – Juancho Dec 5 at 18:11
up vote 2 down vote accepted

In a similar, 1 year old, question on Reddit a solution is presented:

You need to sort columns individually in the opposite order of priority.

Example: You have following table:

| C1 | C2 | C3     |
|----+----+--------|
| c  |  1 | true 6 |
| b  |  2 | true 5 |
| a  |  3 | true 3 |
| a  |  2 | true 2 |
| b  |  1 | true 4 |
| a  |  1 | true 1 |

If you want this result:

| C1 | C2 | C3     |
|----+----+--------|
| a  |  1 | true 1 |
| a  |  2 | true 2 |
| a  |  3 | true 3 |
| b  |  1 | true 4 |
| b  |  2 | true 5 |
| c  |  1 | true 6 |

You have to sort the table in following order:

  1. Column: C2 ; sort order n (numeric)
  2. Column: C1 ; sort order a (alphabetic)

If you want instead this result:

| C1 | C2 | C3     |
|----+----+--------|
| a  |  1 | true 1 |
| b  |  1 | true 4 |
| c  |  1 | true 6 |
| a  |  2 | true 2 |
| b  |  2 | true 5 |
| a  |  3 | true 3 |

You need to do this sort order:

  1. Column C1 ; sort order a (alphabetic)
  2. Column C2 ; sort order n (numeric)
  • "For example, to sort by column A, then by column B for equal As, then by column C for equal As and Bs, just sort the table by column C, then by column B, then by column A." - what does it mean B for equal As ? – Alexei Dec 6 at 16:25
  • @Alexei see example in my answer – jue Dec 6 at 17:04

Your Answer

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.