Properly sort chapter list by chapter number

The sort order was switched and instead of ASC it was sorted DESC and vice versa.
This was caused because the server returned the list (most of the time) in ASC order by chapter number.

Additionally, the  list was overall never properly sorted by chapter number and only worked because the server (most of the time) returned it in the correct order by chapter number.
However, this is not guaranteed at all since the server sorts in ASC order by the chapter id by default
This commit is contained in:
schroda
2024-06-28 11:13:00 +02:00
parent dd6b0a1e44
commit f52b634da1

View File

@@ -22,7 +22,7 @@ const defaultChapterOptions: ChapterListOptions = {
unread: undefined, unread: undefined,
downloaded: undefined, downloaded: undefined,
bookmarked: undefined, bookmarked: undefined,
reverse: false, reverse: true,
sortBy: 'source', sortBy: 'source',
showChapterNumber: false, showChapterNumber: false,
}; };
@@ -88,14 +88,14 @@ export function filterAndSortChapters(chapters: TChapter[], options: ChapterList
bookmarkedFilter(options.bookmarked, chp), bookmarkedFilter(options.bookmarked, chp),
) )
: [...chapters]; : [...chapters];
const Sorted = const sorted =
options.sortBy === 'fetchedAt' options.sortBy === 'fetchedAt'
? filtered.sort((a, b) => Number(a.fetchedAt ?? 0) - Number(b.fetchedAt ?? 0)) ? filtered.toSorted((a, b) => Number(a.fetchedAt ?? 0) - Number(b.fetchedAt ?? 0))
: filtered; : filtered.toSorted((a, b) => a.chapterNumber - b.chapterNumber);
if (options.reverse) { if (options.reverse) {
Sorted.reverse(); sorted.reverse();
} }
return Sorted; return sorted;
} }
export const useChapterOptions = (mangaId: number) => export const useChapterOptions = (mangaId: number) =>