diff --git a/src/m_menu.c b/src/m_menu.c index 58fa803fd..0f86d2300 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -6872,6 +6872,32 @@ static void M_HutCheckReplays(size_t maxnum) } } +static int ReplayListSortComparator(const void *entry1, const void *entry2) +{ + const menudemo_t *demo1 = (const menudemo_t*)entry1; + const menudemo_t *demo2 = (const menudemo_t*)entry2; + + char filepath1[sizeof(demo1->filepath)]; + char filepath2[sizeof(demo2->filepath)]; + + // First check for directories, they always should be at the top + if (demo1->type == MD_SUBDIR && demo2->type != MD_SUBDIR) + return -1; + else if (demo2->type == MD_SUBDIR && demo1->type != MD_SUBDIR) + return 1; + else if (demo1->type == MD_SUBDIR && demo2->type == MD_SUBDIR) + return 0; + + memcpy(filepath1, demo1->filepath, sizeof(filepath1)); + memcpy(filepath2, demo2->filepath, sizeof(filepath2)); + + nameonly(filepath1); + nameonly(filepath2); + + // Comparing in opposite order to move new replays to the top + return strncmp(filepath2, filepath1, sizeof(filepath1)); +} + static void PrepReplayList(boolean reset) { size_t i; @@ -6915,6 +6941,8 @@ static void PrepReplayList(boolean reset) } } + qs22j(demolist_all, sizedirmenu, sizeof(menudemo_t), ReplayListSortComparator); + Unlock_search_state(); LoadReplayNames();