Allow to sort numerically

One thing that I’m really missing is the ability to sort cards numerically in Anki. This includes the order of cards shown in the card browser and the order in which they’re presented when learning.

Anki can do numerical sorting only if the field contains only numbers, e.g. 1.

The sorting is done at the database level, which is only able to sort in numeric order if the fields contain nothing but digits. - Damien Elmes

But a field often contains alphanumeric characters when needing to distinguish several “levels”, e.g. L1E1. Currently, Anki orders it alphanumerically, which is usually not what one expect, e.g.

L1E1
L1E2
..
L10E1
L10E2
..
L11E1
L11E2
..

...

L2E1
L2E2
..
L20E1
L20E2
..
L21E1
L21E2
..

This seems to be a common pain point for many people judging from the search results on the Web for “anki sort numerical” or similar.

The sort command line utility can do numerical sorting using the sort -n command [1] [2]. Anki should be able to do it as well.


  1. “-n, --numeric-sort compare according to string numerical value”. Source: sort(1) - Linux manual page ↩︎

  2. ls | sort -n. Source: ls - List files sorted numerically - Unix & Linux Stack Exchange ↩︎

I think what this means is that when the database is queried for results, and asked for a sorted result, it can only provide such a sorting for numerical fields. This depends on the database backend, not really on the Anki code.

One thing that I’m really missing is the ability to sort cards numerically in Anki. This includes the order of cards shown in the card browser and the order in which they’re presented when learning.

Anki can do numerical sorting only if the field contains only numbers, e.g. 1.

The sorting is done at the database level, which is only able to sort in numeric order if the fields contain nothing but digits.

But a field often contains alphanumeric characters when needing to distinguish several “levels”, e.g. L1E1. Currently, Anki orders it alphanumerically, which is usually not what one expect, e.g.

L1E1
L1E2
..
L10E1
L10E2
..
L11E1
L11E2
..

...

L2E1
L2E2
..
L20E1
L20E2
..
L21E1
L21E2
..

This seems to be a common pain point for many people judging from the search results on the Web for “anki sort numerical” or similar.

The sort command line utility can do numerical sorting using the sort -n command [1] [2]. Anki should be able to do it as well.


  1. “-n, --numeric-sort compare according to string numerical value”. Source: sort(1) - Linux manual page ↩︎

  2. ls | sort -n. Source: ls - List files sorted numerically - Unix & Linux Stack Exchange ↩︎

Do you know in advance the highest-order digit needed to accommodate your likely highest alphanumeric value?

For example, if the order is 3, then you can do the following:

L001E001
L001E002

L010E001
L010E002

L011E001
L011E002


L002E001
L002E002

L020E001
L020E002

L021E001
L021E002

.
If there are no errors in the above, I shall be most surprised.

Thanks.

1 Like

I found a workaround. I added a new field in my custom note type, named it ID, and give each new card its specific umber. This way i am able to sort my cards numerically.

I came to the forum to ask something similar. In my case, about half of my decks sort into something like this:

  • Psalm 103:10-12
  • Psalm 107:1
  • Psalm 10:4
  • Psalm 11:1
  • Psalm 122:1
  • Psalm 139:7-10
  • Psalm 19:1-3
  • Psalm 1:1-2

The comment posted above about how sorting is done at the database level is helpful for understanding the challenge. As a PHP programmer, I would look to solve this by passing the results from the database (mysql in my case) through natsort() (or similar for arrays). It would be an easy “fix.”

Does such a function exist for the language Anki is written in? If it could be something as straightforward as that, I would love for it to be implemented. However, not knowing the language nor how the code is set up, I would be of the opinion to leave it alone if it is not a simple change.