From 6617b3fe4e244d9558e3d222da511b203600df04 Mon Sep 17 00:00:00 2001 From: nova Date: Sat, 10 May 2025 22:30:52 +0200 Subject: [PATCH] added natural sort --- backend.c | 2 ++ sorting.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/backend.c b/backend.c index 34e8574..b4c2b5b 100644 --- a/backend.c +++ b/backend.c @@ -39,6 +39,7 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten scandir(path, &entry, skip_hidden_files, alphasort); } + unsigned long i = 0; for (i = 0; i < *dir_file_count; i++ ) { if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) { @@ -99,6 +100,7 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten free(file); } } + qsort(dir_content, *dir_file_count, sizeof(file), sort_natural); for (i = 0; i < *dir_file_count; i++) { free(entry[i]); diff --git a/sorting.c b/sorting.c index 5f08601..41f39b1 100644 --- a/sorting.c +++ b/sorting.c @@ -1,5 +1,6 @@ #include #include +#include #include "defines.h" extern unsigned int settings; @@ -12,3 +13,17 @@ int skip_hidden_files(const struct dirent *entry){ return 1; } +int sort_natural(const void *file0, const void *file1){ + unsigned char file_type0 = ((file*)file0)->file_type; + unsigned char file_type1 = ((file*)file1)->file_type; + if (file_type0 > file_type1) { + return 1; + } else if (file_type0 < file_type1) { + return -1; + } else { + char *file_name0 = ((file*)file0)->file_name; + char *file_name1 = ((file*)file1)->file_name; + return strcasecmp(file_name0, file_name1); + } + +}