AudioAnchor
AudioAnchor copied to clipboard
Natural Sorting
currently items are not sorted by natural sort which leads to:
item episode 0 promo
item episode 1 stardust
item episode 10 the secret of mystic mountain
item episode 11 ...
...
item episode 19 ...
item episode 2 ...
item episode 20 ...
i assume that #5 was already released, i am currently on version 2.2 but i also asume that the CAST fix only work for numbers at the beginning and the end (or do i miss something?).
maybe helpful:
- https://sqlite.org/forum/forumpost/e4dc6f3331
maybe a fix:
- https://github.com/paour/natorder
- https://github.com/jagobagascon/Natural-Sorting-for-Java
I have two solutions to this issue at hacky-filesort and database-filesort.
The reasons for not making (one of) them a pull request are:
-
The hacky one is ... well hacky in the sense that the ordering is computed (in java) on the fly every time it is needed. It works actually quite well (on my own phone) and touches not too much existing code -- but it is only a workaround, or "symptomatic treatment" ...
(Ideally, the sorting would be done by the database backend, but as far as i can see, this is not easily possible: SQLite does not support this natively, and sqlite3_create_collation does not seem to be exposed in the java/android classes ...)
-
The other one is not thoroughly tested.
It works by adding an additional column to the database table for audio files, called
sort_index. EachSynchronizeof the database will (if necessary) trigger recomputation of the album's file order according to natural sort. Later, the database query can simplyORDER BYthe new column.It seems to work fine, but i only quickly tested it in the emulator on a couple of files ... However, this approach is a lot cleaner and i don't see a reason why it shouldn't be merged in principle.
Warning: If you want to try this, make a backup of your database first (
Export database...), because the database will be upgraded automatically (and then be incompatible with the official app) and there is no downgrade implemented.(After the automatic database upgrade, a manual
Synchronizeis necessary to populate thesort_index.)
Additional notes:
- In both branches, i bumped the minimum Android API to 24. This is because of the use of
android.icu.text.RuleBasedCollatorfor natural sort. While i would always prefer not to re-invent the wheel, the minimum API version could probably be preserved by using one of the available ad-hoc java classes for natural sort (e.g. as mentioned by OP) ... - In the second approach, i refactored
Synchronizer::updateAudioFileTablequite a bit (to include thesort_indexgeneration), while i tried to preserve the semantics of the previous code ... - @flackbash Feel free to use/test/merge/comment on/... any of this. :)