From 3c093f48ec8229b9ce45acbf919692cec63a503c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20K=C3=BCnzel?= <simonk@fsmpi.rwth-aachen.de>
Date: Sat, 26 Oct 2024 22:00:18 +0200
Subject: [PATCH] Fix video suggestion lecture order, Closes #81

---
 src/components/Player.tsx | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/components/Player.tsx b/src/components/Player.tsx
index a6cf6b3..d633aca 100644
--- a/src/components/Player.tsx
+++ b/src/components/Player.tsx
@@ -609,10 +609,14 @@ function Chapters({ chapters, seekTo }: { chapters?: chapter[]; seekTo: (time: n
 function VideoSuggestions({ course, lecture }: { course: course; lecture: lecture }) {
     const { language } = useLanguage();
     // find previous and next lecture
-    const prevLecture = course.lectures?.findLast(
+    const sortedLectures = course.lectures?.sort((a, b) => {
+        // time looks like this: 2015-10-20T12:15:00
+        return new Date(a.time).valueOf() - new Date(b.time).valueOf();
+    });
+    const prevLecture = sortedLectures?.findLast(
         (l) => new Date(l.time) < new Date(lecture.time) && (l.media_sources ?? []).length > 0,
     );
-    const nextLecture = course.lectures?.find(
+    const nextLecture = sortedLectures?.find(
         (l) => new Date(l.time) > new Date(lecture.time) && (l.media_sources ?? []).length > 0,
     );
 
-- 
GitLab