From 1b79b3f42d0cd7f6dddf1fa62514afb8ad972e3a Mon Sep 17 00:00:00 2001 From: advplyr Date: Fri, 22 Apr 2022 17:11:03 -0500 Subject: [PATCH] Add secondary sort by series sort title when sorting by author #274 --- server/controllers/LibraryController.js | 25 +++++++++++++++++++++---- server/objects/metadata/BookMetadata.js | 5 +++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index cbc314cf..b1850248 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -176,12 +176,29 @@ class LibraryController { sortKey += 'IgnorePrefix' } + // Start sort var direction = payload.sortDesc ? 'desc' : 'asc' - libraryItems = naturalSort(libraryItems)[direction]((li) => { + var sortArray = [ + { + [direction]: (li) => { + // Supports dot notation strings i.e. "media.metadata.title" + return sortKey.split('.').reduce((a, b) => a[b], li) + } + } + ] - // Supports dot notation strings i.e. "media.metadata.title" - return sortKey.split('.').reduce((a, b) => a[b], li) - }) + // Secondary sort when sorting by book author use series sort title + if (payload.mediaType === 'book' && payload.sortBy.includes('author')) { + sortArray.push({ + asc: (li) => { + if (li.media.metadata.series && li.media.metadata.series.length) { + return li.media.metadata.getSeriesSortTitle(li.media.metadata.series[0]) + } + return null + } + }) + } + libraryItems = naturalSort(libraryItems).by(sortArray) } // TODO: Potentially implement collapse series again diff --git a/server/objects/metadata/BookMetadata.js b/server/objects/metadata/BookMetadata.js index c5bedc66..59204777 100644 --- a/server/objects/metadata/BookMetadata.js +++ b/server/objects/metadata/BookMetadata.js @@ -162,6 +162,11 @@ class BookMetadata { if (!series) return null return series.sequence || '' } + getSeriesSortTitle(series) { + if (!series) return '' + if (!series.sequence) return series.name + return `${series.name} #${series.sequence}` + } update(payload) { var json = this.toJSON()