Skip to content

feat(subsonic): Sorting like file explorers - #710

Open
ummshsh wants to merge 2 commits into
sentriz:masterfrom
ummshsh:feature/sort-key
Open

feat(subsonic): Sorting like file explorers#710
ummshsh wants to merge 2 commits into
sentriz:masterfrom
ummshsh:feature/sort-key

Conversation

@ummshsh

@ummshsh ummshsh commented Jul 5, 2026

Copy link
Copy Markdown

Good day!

I thought it makes sense to have option to see folders sorted just like you see them in file explorers. Certainly makes my live easier =)

Problem

Gonic sorts folder listings by right_path COLLATE NOCASE — raw bytewise comparison. This means ( sorts before _ (ASCII 40 vs 95).
File explorers (Finder, Explorer, Nautilus) use the Unicode Collation Algorithm, which gives _ a lower weight than ( — so _1 comes before (1992) One.

Solution

Pre-computed sort key column — same approach file explorers use under the hood.
linguistic (UCA-based) folder sorting with admin toggle

Screenshot 2026-07-05 at 18 17 56

What changed (10 files)

  • db/db.go — Added RightPathSortKey field to Album model + new LinguisticSorting setting key
  • db/migrations.go — Migration 202607051200 adds the column via AutoMigrate and backfills sort keys for all existing albums using golang.org/x/text/collate
  • scanner/scanner.gopopulateAlbumBasics always writes a UCA sort key from the directory basename during scan
  • server/ctrlsubsonic/ctrl.go — Three helper methods (sortRightPath, sortRightPathPrefixed, sortRightPathParentPrefixed) that check the linguistic_sorting setting and return either right_path_sort_key or right_path COLLATE NOCASE
  • server/ctrlsubsonic/handlers_by_folder.go — All 6 ORDER BY clauses switched to use helpers
  • server/ctrlsubsonic/handlers_by_tags.go — Tag-mode artist album listing switched to use helper
  • server/ctrladmin/handlers.go — Loads setting in ServeHome, added POST handler to save toggle
  • server/ctrladmin/ctrl.go — Registered route, added LinguisticSorting to template data
  • server/ctrladmin/adminui/pages/home.tmpl — New "sorting" block with dropdown (bytewise / linguistic)
  • go.mod — Promoted golang.org/x/text to direct dependency

How it works

The sort key is always written during scanning regardless of the toggle. The toggle (/admin/homesorting dropdown) only changes which column the ORDER BY uses at query time — zero-rebuild, instant switch. Migration backfills existing albums so there's no gap.

Migration behavior

On upgrade, migrateAlbumRightPathSortKey:

  1. Adds right_path_sort_key TEXT column via AutoMigrate
  2. Iterates all albums and sets the sort key from right_path using UCA
  3. No downtime — column is nullable, next scan fills any newly added albums

@ummshsh

ummshsh commented Jul 5, 2026

Copy link
Copy Markdown
Author

This approach targets only folders, I don't know if it makes sense to do the same for individual tracks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant