Introduce new book sorting mode: Author first, then Series, then Series Index (#1620)
This commit is contained in:
@@ -8,6 +8,7 @@ export class BookSorter {
|
|||||||
{ label: 'Title + Series', field: 'titleSeries', direction: SortDirection.ASCENDING },
|
{ label: 'Title + Series', field: 'titleSeries', direction: SortDirection.ASCENDING },
|
||||||
{ label: 'File Name', field: 'fileName', direction: SortDirection.ASCENDING },
|
{ label: 'File Name', field: 'fileName', direction: SortDirection.ASCENDING },
|
||||||
{ label: 'Author', field: 'author', direction: SortDirection.ASCENDING },
|
{ label: 'Author', field: 'author', direction: SortDirection.ASCENDING },
|
||||||
|
{ label: 'Author + Series', field: 'authorSeries', direction: SortDirection.ASCENDING },
|
||||||
{ label: 'Last Read', field: 'lastReadTime', direction: SortDirection.ASCENDING },
|
{ label: 'Last Read', field: 'lastReadTime', direction: SortDirection.ASCENDING },
|
||||||
{ label: 'Added On', field: 'addedOn', direction: SortDirection.ASCENDING },
|
{ label: 'Added On', field: 'addedOn', direction: SortDirection.ASCENDING },
|
||||||
{ label: 'File Size', field: 'fileSizeKb', direction: SortDirection.ASCENDING },
|
{ label: 'File Size', field: 'fileSizeKb', direction: SortDirection.ASCENDING },
|
||||||
|
|||||||
@@ -61,6 +61,17 @@ export class SortService {
|
|||||||
return [title, Number.MAX_SAFE_INTEGER];
|
return [title, Number.MAX_SAFE_INTEGER];
|
||||||
},
|
},
|
||||||
author: (book) => book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null,
|
author: (book) => book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null,
|
||||||
|
authorSeries: (book) => {
|
||||||
|
const author = book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null;
|
||||||
|
const series = book.metadata?.seriesName?.toLowerCase() || null;
|
||||||
|
const seriesNumber = book.metadata?.seriesNumber ?? Number.MAX_SAFE_INTEGER;
|
||||||
|
const title = book.metadata?.title?.toLowerCase() || '';
|
||||||
|
if (series) {
|
||||||
|
return [author, series, seriesNumber, title];
|
||||||
|
}
|
||||||
|
// For books without a series, use a very large string for series name to sort them last within an author.
|
||||||
|
return [author, '~~~~~~~~~~~~~~~~~', Number.MAX_SAFE_INTEGER, title];
|
||||||
|
},
|
||||||
publishedDate: (book) => {
|
publishedDate: (book) => {
|
||||||
const date = book.metadata?.publishedDate;
|
const date = book.metadata?.publishedDate;
|
||||||
return date === null || date === undefined ? null : new Date(date).getTime();
|
return date === null || date === undefined ? null : new Date(date).getTime();
|
||||||
@@ -83,7 +94,7 @@ export class SortService {
|
|||||||
lastReadTime: (book) => book.lastReadTime ? new Date(book.lastReadTime).getTime() : null,
|
lastReadTime: (book) => book.lastReadTime ? new Date(book.lastReadTime).getTime() : null,
|
||||||
addedOn: (book) => book.addedOn ? new Date(book.addedOn).getTime() : null,
|
addedOn: (book) => book.addedOn ? new Date(book.addedOn).getTime() : null,
|
||||||
fileSizeKb: (book) => book.fileSizeKb || null,
|
fileSizeKb: (book) => book.fileSizeKb || null,
|
||||||
fileName:(book) => book.fileName,
|
fileName: (book) => book.fileName,
|
||||||
};
|
};
|
||||||
|
|
||||||
applySort(books: Book[], selectedSort: SortOption | null): Book[] {
|
applySort(books: Book[], selectedSort: SortOption | null): Book[] {
|
||||||
@@ -101,12 +112,31 @@ export class SortService {
|
|||||||
const aValue = extractor(a);
|
const aValue = extractor(a);
|
||||||
const bValue = extractor(b);
|
const bValue = extractor(b);
|
||||||
|
|
||||||
let result: number;
|
let result = 0;
|
||||||
|
|
||||||
if (Array.isArray(aValue) && Array.isArray(bValue)) {
|
if (Array.isArray(aValue) && Array.isArray(bValue)) {
|
||||||
// For titleSeries: [seriesName, seriesNumber]
|
for (let i = 0; i < aValue.length; i++) {
|
||||||
const nameCompare = this.naturalCompare(aValue[0] || '', bValue[0] || '');
|
const valA = aValue[i];
|
||||||
result = nameCompare !== 0 ? nameCompare : (aValue[1] - bValue[1]);
|
const valB = bValue[i];
|
||||||
|
|
||||||
|
if (typeof valA === 'string' && typeof valB === 'string') {
|
||||||
|
result = this.naturalCompare(valA, valB);
|
||||||
|
if (result !== 0) break;
|
||||||
|
} else if (typeof valA === 'number' && typeof valB === 'number') {
|
||||||
|
result = valA - valB;
|
||||||
|
if (result !== 0) break;
|
||||||
|
} else {
|
||||||
|
if (valA == null && valB != null) {
|
||||||
|
result = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (valA != null && valB == null) {
|
||||||
|
result = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
result = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (typeof aValue === 'string' && typeof bValue === 'string') {
|
} else if (typeof aValue === 'string' && typeof bValue === 'string') {
|
||||||
result = this.naturalCompare(aValue, bValue);
|
result = this.naturalCompare(aValue, bValue);
|
||||||
} else if (typeof aValue === 'number' && typeof bValue === 'number') {
|
} else if (typeof aValue === 'number' && typeof bValue === 'number') {
|
||||||
|
|||||||
Reference in New Issue
Block a user