# Is it possible to sort by two columns?

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?

• `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. Commented Dec 5, 2018 at 18:11

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 ? Commented Dec 6, 2018 at 16:25
• @Alexei see example in my answer
– jue
Commented Dec 6, 2018 at 17:04