Recursively sort filelist

This commit is contained in:
2024-10-29 21:38:36 +01:00
parent ad3fa28bc7
commit 91f0ebe99c

View File

@@ -6,6 +6,7 @@ import (
"novamd/internal/gitutils" "novamd/internal/gitutils"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
) )
@@ -82,26 +83,57 @@ func (fs *FileSystem) walkDirectory(dir, prefix string) ([]FileNode, error) {
return nil, err return nil, err
} }
nodes := make([]FileNode, 0) // Split entries into directories and files
var dirs, files []os.DirEntry
for _, entry := range entries { for _, entry := range entries {
if entry.IsDir() {
dirs = append(dirs, entry)
} else {
files = append(files, entry)
}
}
// Sort directories and files separately
sort.Slice(dirs, func(i, j int) bool {
return strings.ToLower(dirs[i].Name()) < strings.ToLower(dirs[j].Name())
})
sort.Slice(files, func(i, j int) bool {
return strings.ToLower(files[i].Name()) < strings.ToLower(files[j].Name())
})
// Create combined slice with directories first, then files
nodes := make([]FileNode, 0, len(entries))
// Add directories first
for _, entry := range dirs {
name := entry.Name() name := entry.Name()
path := filepath.Join(prefix, name) path := filepath.Join(prefix, name)
fullPath := filepath.Join(dir, name) fullPath := filepath.Join(dir, name)
children, err := fs.walkDirectory(fullPath, path)
if err != nil {
return nil, err
}
node := FileNode{
ID: path,
Name: name,
Path: path,
Children: children,
}
nodes = append(nodes, node)
}
// Then add files
for _, entry := range files {
name := entry.Name()
path := filepath.Join(prefix, name)
node := FileNode{ node := FileNode{
ID: path, ID: path,
Name: name, Name: name,
Path: path, Path: path,
} }
if entry.IsDir() {
children, err := fs.walkDirectory(fullPath, path)
if err != nil {
return nil, err
}
node.Children = children
}
nodes = append(nodes, node) nodes = append(nodes, node)
} }