From d4cd6f52f425be1f04fb2b3f8c9074c25f3c539e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= <127139797+balazs-szucs@users.noreply.github.com> Date: Mon, 19 Jan 2026 02:31:45 +0100 Subject: [PATCH] feat(sort): Add sort by author surname and first name (#2292) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../components/book-browser/sorting/BookSorter.ts | 1 + .../app/features/book/service/sort.service.spec.ts | 11 +++++++++++ .../src/app/features/book/service/sort.service.ts | 7 +++++++ 3 files changed, 19 insertions(+) diff --git a/booklore-ui/src/app/features/book/components/book-browser/sorting/BookSorter.ts b/booklore-ui/src/app/features/book/components/book-browser/sorting/BookSorter.ts index 0cf77c963..a64400e29 100644 --- a/booklore-ui/src/app/features/book/components/book-browser/sorting/BookSorter.ts +++ b/booklore-ui/src/app/features/book/components/book-browser/sorting/BookSorter.ts @@ -8,6 +8,7 @@ export class BookSorter { {label: 'Title + Series', field: 'titleSeries', direction: SortDirection.ASCENDING}, {label: 'File Name', field: 'fileName', direction: SortDirection.ASCENDING}, {label: 'Author', field: 'author', direction: SortDirection.ASCENDING}, + {label: 'Author (Surname)', field: 'authorSurnameVorname', direction: SortDirection.ASCENDING}, {label: 'Author + Series', field: 'authorSeries', direction: SortDirection.ASCENDING}, {label: 'Last Read', field: 'lastReadTime', direction: SortDirection.ASCENDING}, {label: 'Personal Rating', field: 'personalRating', direction: SortDirection.ASCENDING}, diff --git a/booklore-ui/src/app/features/book/service/sort.service.spec.ts b/booklore-ui/src/app/features/book/service/sort.service.spec.ts index 7c1d15e91..415b5cab3 100644 --- a/booklore-ui/src/app/features/book/service/sort.service.spec.ts +++ b/booklore-ui/src/app/features/book/service/sort.service.spec.ts @@ -85,6 +85,17 @@ describe('SortService', () => { expect(sorted.map(b => b.metadata?.authors?.[0])).toEqual(['Alice', 'Alice', 'Bob']); }); + it('should sort by author surname', () => { + const testBooks = [ + makeBook({ id: 1, metadata: { bookId: 1, title: 'T1', authors: ['Terry Pratchett'] } }), + makeBook({ id: 2, metadata: { bookId: 2, title: 'T2', authors: ['Neil Gaiman'] } }), + makeBook({ id: 3, metadata: { bookId: 3, title: 'T3', authors: ['Stephen King'] } }), + ]; + + const sorted = service.applySort(testBooks, { field: 'authorSurnameVorname', direction: SortDirection.ASCENDING, label: 'Author (Surname)' }); + expect(sorted.map(b => b.metadata?.authors?.[0])).toEqual(['Neil Gaiman', 'Stephen King', 'Terry Pratchett']); + }); + it('should sort by publishedDate descending', () => { const sorted = service.applySort(books, {field: 'publishedDate', direction: SortDirection.DESCENDING, label: 'Published Date'}); expect(sorted.map(b => b.metadata?.title)).toEqual(['Book 1', 'Book 10', 'Book 2']); diff --git a/booklore-ui/src/app/features/book/service/sort.service.ts b/booklore-ui/src/app/features/book/service/sort.service.ts index 9820fcda5..0bd2a66f0 100644 --- a/booklore-ui/src/app/features/book/service/sort.service.ts +++ b/booklore-ui/src/app/features/book/service/sort.service.ts @@ -61,6 +61,13 @@ export class SortService { return [title, Number.MAX_SAFE_INTEGER]; }, author: (book) => book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null, + authorSurnameVorname: (book) => book.metadata?.authors?.map(a => { + const parts = a.trim().split(/\s+/); + if (parts.length < 2) return a.toLowerCase(); + const surname = parts.pop(); + const firstname = parts.join(" "); + return `${surname}, ${firstname}`.toLowerCase(); + }).join(", ") || null, authorSeries: (book) => { const author = book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null; const series = book.metadata?.seriesName?.toLowerCase() || null;