Sorting must apply to the **whole** result set, not to whatever is loaded in the client. - [ ] `AlbumSort`: `RELEVANCE`, `TITLE_ASC`, `TITLE_DESC`, `RELEASE_YEAR_ASC`, `RELEASE_YEAR_DESC`, `AVERAGE_RATING_ASC`, `AVERAGE_RATING_DESC` - [ ] `album.average_rating` maintained by a trigger on `review`, so the sort key is a stored column - [ ] `AVERAGE_RATING_*` sorts on that column - [ ] every sort has matching keyset cursor columns so paging stays stable - [ ] sort control in the toolbar, value in the URL **Acceptance:** with more albums loaded than fit on screen, switching sort re-queries and the order is correct across page boundaries. ## Names The checklist uses the names `schema.graphql` already has. An earlier draft of this issue said `YEAR_ASC/DESC` and `RATING_ASC/DESC`; those were never the names, and it is `RELEASE_YEAR` and `AVERAGE_RATING`. Extend the existing enum rather than renaming it. ## Two of the seven are done `TITLE_ASC` and `RELEASE_YEAR_DESC` both work end to end. Both sort in SQL over the whole result set, both have keyset cursors with an `id` tiebreak and nulls last in both directions, and both are covered in `server/src/search/search.test.ts`. ## The rating sort needs a stored column, not an aggregate This used to say the rating sorts run on `AVG(review.rating)` computed in SQL. That is wrong and a peer reviewer caught it. Keyset pagination needs the sort key to be a stable value you can seek against, and a live aggregate is neither cheap nor stable: it recomputes for the whole result set, and it moves every time anyone writes a review, so a cursor handed out a moment ago can point into the middle of a reshuffled order. Store it instead. Add `album.average_rating` and keep it current with a trigger on `review`, the same way `set_review_updated_at()` keeps `updated_at` current and `album_search_vector_before_write` keeps `search_vector` current. Then the rating sorts are ordinary column sorts with the same cursor shape as the others. Two things fall out of this that are worth doing in the same change: - **`Album.averageRating` stops returning null.** `schema.graphql` declares it and `client/src/api/searchAlbums.ts` already selects it, but `searchAlbums` never computes it. A maintained column makes the field actually resolve. - **Ties matter more here than for the other sorts.** Rating has far more duplicates than a title does, so the `id` tiebreak is doing real work and deserves an explicit test. Aggregation is scoped to a single album per review write, so the trigger cost is bounded and independent of catalog size. That is the property that makes this safe for an unknown data volume, which is the reason #22 exists. `AVERAGE_RATING_DESC` is in the enum but `searchAlbums` throws `GraphQLError('Sorting by rating is not available until reviews are stored.')` today. That error is the right behaviour until #15 stores reviews, and it can be removed once the column and the trigger land. ## Client half Sort control in the toolbar, with the value in the query string. The param name is already settled: `#19` owns `sort` through `useSearchState`, and `setSort` is the setter. Use that rather than inventing a second one.