From 3f507e0a3099d688b20bb84955415dd5548e62bf Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Mon, 29 Apr 2024 12:37:11 +0200 Subject: [PATCH] Keep order of chapters when removing duplicates --- src/lib/data/Chapters.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/lib/data/Chapters.ts b/src/lib/data/Chapters.ts index f6ab754d..e1be4741 100644 --- a/src/lib/data/Chapters.ts +++ b/src/lib/data/Chapters.ts @@ -320,15 +320,18 @@ export class Chapters { static removeDuplicates(currentChapter: T, chapters: T[]): T[] { const chapterNumberToChapters = Object.groupBy(chapters, ({ chapterNumber }) => chapterNumber); - return Object.values(chapterNumberToChapters) - .map( - (groupedChapters) => - // the result of groupBy can't result in undefined values - groupedChapters!.find((chapter) => chapter.id === currentChapter.id) ?? - groupedChapters!.findLast((chapter) => chapter.scanlator === currentChapter.scanlator) ?? - groupedChapters!.slice(-1)[0], - ) - .sort((a, b) => b.chapterNumber - a.chapterNumber); + const uniqueChapters = Object.values(chapterNumberToChapters).map( + (groupedChapters) => + // the result of groupBy can't result in undefined values + groupedChapters!.find((chapter) => chapter.id === currentChapter.id) ?? + groupedChapters!.findLast((chapter) => chapter.scanlator === currentChapter.scanlator) ?? + groupedChapters!.slice(-1)[0], + ); + + // keep the chapters in the same order as they were passed + return chapters + .map(({ id }) => uniqueChapters.find((chapter) => chapter.id === id)) + .filter((chapter): chapter is T => !!chapter); } static getNextChapter(