Skip to content

Add sorting by book ratings and numeric custom columns - #1668

Open
ecliseice wants to merge 1 commit into
crocodilestick:mainfrom
ecliseice:feature/rating-sorting
Open

ecliseice wants to merge 1 commit into
crocodilestick:mainfrom
ecliseice:feature/rating-sorting

Conversation

@ecliseice

@ecliseice ecliseice commented Nov 11, 2026 •

Copy link
Copy Markdown

Problem

CWA stores a standard Calibre rating and numeric custom columns, but the cover catalog and the Books List cannot order the whole library by those values. A rating can be used to group books, and the table headers for int and float custom columns are not sortable.

Solution

Add one server-side sort for the Calibre rating and for every custom column of datatype int or float. The order is applied to the existing paged queries, before pagination, as a correlated scalar subquery. That keeps the current joins from duplicating rows. Column ids are checked against the library metadata and are never interpolated into SQL.

Features

  • Ascending and descending order. In the Books List the first click sorts high to low.
  • The catalog dropdown is built from the library: the Calibre rating, plus each numeric custom column under its own name. A column such as #goodreads_rating appears automatically. A column of another datatype does not.
  • The same menu is on the cover catalog, search results, and the other book lists that already share that sort toolbar (author, series, publisher, language, category, format, ratings, read/unread, downloaded, archived, top rated, magic shelves).
  • Books List: a read-only Rating column, and sortable headers for int and float custom columns.
  • Books with no rating or no custom-column value stay last in both directions. A stored Calibre rating of 0 means unrated. 0 in a custom column is a real value.
  • Title sort and book id keep equal values in a stable order across pages.
  • The chosen catalog order is stored with the other view settings. An unknown or non-numeric key falls back to the default order and is not kept as the stored key.
  • Hot Books, Discover, and ordinary shelves are unchanged. Shelf sorting still rewrites the manual shelf order, so it is left alone.
  • No metadata database migration and no change to stored ratings.

Testing

  • tests/unit/test_rating_sort.py (73 tests): both directions for the rating and for int/float columns, missing values last, 0 in a custom column kept in order, an empty column keeping title order, several columns, a library with no custom columns, paging of the whole library, a filter plus paging, the series join used by the book lists, stable order when only the first sort expression is applied, ignored and read-only columns, invalid keys including SQL fragments, and list_books for ratings and custom_column_<id>
  • pytest -m "smoke or unit" locally: 347 passed, 24 skipped
  • The same fast suite on the fork: Test Suite run
  • Page timing on a 10,000-book library stays in the same range as the existing sorts (about 11–24 ms per page)
  • A temporary container on a copy of a real library: cover catalog, search, author list, and Books List (page 1, page 2, and search) for the rating and a numeric column
  • Reviewer: confirm the dropdown matches the sort toolbar in the current theme

The catalog and Books List can store a Calibre rating and numeric custom columns, but could not order the whole library by those values.

Co-authored-by: Cursor <cursoragent@cursor.com>
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