From 906024fca24ad99b741ccc96c2f3a0111e2eeaea Mon Sep 17 00:00:00 2001 From: Fabrizio Costa Medich <134924838+FabrizioCostaMedich@users.noreply.github.com> Date: Thu, 7 Nov 2024 15:49:53 +0100 Subject: [PATCH] fix(courses): prioritize sorting by date, if they are the same sort by name in increasing order (#536) --- .../courses/screens/CourseDirectoryScreen.tsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/features/courses/screens/CourseDirectoryScreen.tsx b/src/features/courses/screens/CourseDirectoryScreen.tsx index 5c86cfb2..62c086fc 100644 --- a/src/features/courses/screens/CourseDirectoryScreen.tsx +++ b/src/features/courses/screens/CourseDirectoryScreen.tsx @@ -11,6 +11,8 @@ import { CourseDirectory, CourseFileOverview } from '@polito/api-client'; import { useFocusEffect } from '@react-navigation/native'; import { NativeStackScreenProps } from '@react-navigation/native-stack'; +import { DateTime } from 'luxon'; + import { BottomBarSpacer } from '../../../core/components/BottomBarSpacer'; import { useSafeAreaSpacing } from '../../../core/hooks/useSafeAreaSpacing'; import { @@ -59,6 +61,20 @@ export const CourseDirectoryScreen = ({ route, navigation }: Props) => { }); }, [directoryName, navigation, t]); + directoryQuery.data?.sort((a, b) => { + if (a.type !== 'directory' && b.type !== 'directory') { + const dateA = DateTime.fromJSDate(a.createdAt).startOf('minute'); + const dateB = DateTime.fromJSDate(b.createdAt).startOf('minute'); + + if (dateA.equals(dateB)) { + return a.name.toLowerCase().localeCompare(b.name.toLowerCase()); + } + + return dateA > dateB ? -1 : 1; + } + return 0; + }); + return (