From 00c6112a8780d4ad645c5c8ecfba9be65ba6ab53 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20Schr=C3=B6tler?= <niklas@allround.digital>
Date: Mon, 11 Dec 2023 19:47:26 +0100
Subject: [PATCH] FahrplanPanel: Fixed sorting when first stops are at the same
 time

---
 src/panels/Fahrplan/FahrplanPanel.tsx | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/panels/Fahrplan/FahrplanPanel.tsx b/src/panels/Fahrplan/FahrplanPanel.tsx
index fff3715..1b57af8 100644
--- a/src/panels/Fahrplan/FahrplanPanel.tsx
+++ b/src/panels/Fahrplan/FahrplanPanel.tsx
@@ -88,7 +88,18 @@ const FahrplanPanel = (props: {definition: FahrplanPanelDefinition}) => {
       }
 
       // Sort the output
-      newRoutes = newRoutes.sort((a, b) => a.countdown - b.countdown)
+      newRoutes = newRoutes.sort((a, b) => {
+        const diff = a.stops[0].arrival.getTime() - b.stops[0].arrival.getTime();
+
+        if(diff !== 0) {
+          return diff;
+        }
+
+        const latestA = Math.max(...a.stops.map(s => s.arrival.getTime()));
+        const latestB = Math.max(...b.stops.map(s => s.arrival.getTime()));
+
+        return latestA - latestB;
+      })
 
       // Write to the display
       setRoutes(newRoutes);
-- 
GitLab