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 [^man] [^ls]. Anki should be able to do it as well.

[^man]: “-n, --numeric-sort compare according to string numerical value”. Source: sort(1) - Linux manual page
[^ls]: 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 [^man] [^ls]. Anki should be able to do it as well.

[^man]: “-n, --numeric-sort compare according to string numerical value”. Source: sort(1) - Linux manual page
[^ls]: 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