diff --git a/sdcard/3ds/ctruLua/libs/openfile.lua b/sdcard/3ds/ctruLua/libs/openfile.lua index 503af39..0137005 100644 --- a/sdcard/3ds/ctruLua/libs/openfile.lua +++ b/sdcard/3ds/ctruLua/libs/openfile.lua @@ -1,3 +1,15 @@ +-- Sort ctr.fs.list returns (directories first and alphabetical sorting) +local function sort(files) + table.sort(files, function(i, j) + if i.isDirectory and not j.isDirectory then + return true + elseif i.isDirectory == j.isDirectory then + return string.lower(i.name) < string.lower(j.name) + end + end) + return files +end + --- Open a file explorer to select a file. -- string title: title of the file explorer. -- string curdir: the directory to initially open the file explorer in, or nil for the current directory. @@ -20,7 +32,8 @@ return function(title, curdir, exts, type) -- Variables local sel = 1 local scroll = 0 - local files = ctr.fs.list(curdir) + local files = sort(ctr.fs.list(curdir)) + if curdir ~= "/" then table.insert(files, 1, { name = "..", isDirectory = true }) end local newFileName = "" @@ -59,7 +72,7 @@ return function(title, curdir, exts, type) sel = 1 scroll = 0 - files = ctr.fs.list(curdir) + files = sort(ctr.fs.list(curdir)) if curdir ~= "/" then table.insert(files, 1, { name = "..", isDirectory = true }) diff --git a/source/fs.c b/source/fs.c index 6d367cb..f442999 100644 --- a/source/fs.c +++ b/source/fs.c @@ -48,7 +48,7 @@ const char* prefix_path(const char* path) { } /*** -Lists a directory contents. +Lists a directory contents (unsorted). @function list @tparam string path the directory we wants to list the content @treturn table the item list. Each item is a table like: