AudioAnchor icon indicating copy to clipboard operation
AudioAnchor copied to clipboard

Natural Sorting

Open c33s opened this issue 5 years ago • 1 comments

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

c33s avatar Aug 18 '20 10:08 c33s

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. Each Synchronize of the database will (if necessary) trigger recomputation of the album's file order according to natural sort. Later, the database query can simply ORDER BY the 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 Synchronize is necessary to populate the sort_index.)

Additional notes:

  • In both branches, i bumped the minimum Android API to 24. This is because of the use of android.icu.text.RuleBasedCollator for 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::updateAudioFileTable quite a bit (to include the sort_index generation), while i tried to preserve the semantics of the previous code ...
  • @flackbash Feel free to use/test/merge/comment on/... any of this. :)

J0J0 avatar Apr 04 '23 21:04 J0J0