# Add-on loading: use natural sort order

**URL:** <https://forums.ankiweb.net/t/add-on-loading-use-natural-sort-order/17092>\
**Category:** Development\
**Created:** [January 27, 2022, 6:42pm UTC](https://forums.ankiweb.net/t/add-on-loading-use-natural-sort-order/17092 "2022-01-27T18:42:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ijgnd](https://avatars.discourse-cdn.com/v4/letter/i/bbe5ce/32.png) [@ijgnd](https://forums.ankiweb.net/u/ijgnd)\
**Post date:** [January 27, 2022, 6:42pm UTC](https://forums.ankiweb.net/t/add-on-loading-use-natural-sort-order/17092/1 "2022-01-27T18:42:48Z")

</div>

Since [this commit in 2018](https://github.com/ankitects/anki/commit/9a23f0958d8bff8d0df1210c39fa18a92d9105bd) add-ons in Anki are loaded in a predictable order by using `.sort()` on the list of add-on folder names.

I suggest to enhance this so that add-ons with a lower ankiweb id are loaded first. This could be done by replacing `l.sort()` with something like

```
def lpad_ankiweb_ids(name):
    return f"{int(name):012}" if name.isdigit() else name
l.sort(key=lpad_ankiweb_ids)

```

Manually installed add-ons can have non-digits in their name so `l.sort(key=int)` would fail.

I think these two additional lines make sense in Anki because sometimes the load order of add-ons or the order of functions from add-ons appended to gui\_hooks is relevant. At the moment add-on creators have to rely on workarounds for delayed loading like the profile\_did\_init hook etc. But I think a better sort order of the list of addons is preferable.

E.g. I have an add-on that allows allows multi-line rows in the browser. For this I want to truncate the text of long cells. I use the gui\_hook “browser\_did\_fetch\_row” for this. The same hook is used by the advanced browser add-on to add the contents of the additional columns created by the AB add-on. My add-on only works with the AB if the AB fills the cells first, and then my addon truncates them. The AB has the nine digit id `874215009` so my add-on id may not start with 1 or 2 which is pretty common for the common 10 digit ankiweb ids. Ankiweb assigns random(?) ids that can be up to 10 digits long so it’s pretty hard to get the needed id by chance when creating a new add-on.

My proposed change could in theory break add-ons (e.g. a nine digit add-on 9… that assumes to be loaded after a 10 digit add-on 1…). But due to the limitations of the current sorting I doubt that this problem will exist in practice.

@glutanimate (pinged because he was involved in the discussion [Sort addons by meta field by ChrisK91 · Pull Request #225 · ankitects/anki · GitHub](https://github.com/ankitects/anki/pull/225) in 2018 and he has many add-ons on ankiweb).

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [January 29, 2022, 3:20am UTC](https://forums.ankiweb.net/t/add-on-loading-use-natural-sort-order/17092/2 "2022-01-29T03:20:19Z")

</div>

The predictable sorting is more about ensuring you get the same results from run to run, as it makes debugging problems harder if problems only happen intermittently. The sorting is not intended as a way of ensuring one add-on runs before or after another - for that, I’d suggest you add to the fetch\_row hook in the main\_window\_did\_init hook.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [May 1, 2023, 3:16am UTC](https://forums.ankiweb.net/t/add-on-loading-use-natural-sort-order/17092/3 "2023-05-01T03:16:27Z")

</div>

Closed on May 1, 2023, 3:16am UTC


