diff --git a/internal/files/files.go b/internal/files/files.go index ef2e8ab..d86179b 100644 --- a/internal/files/files.go +++ b/internal/files/files.go @@ -5,6 +5,7 @@ import ( "cmp" "io/fs" "path/filepath" + "strconv" "strings" "time" ) @@ -54,15 +55,11 @@ func SortBySizeReverse(a, b File) int { } func SortByName(a, b File) int { - an := strings.ToLower(a.Name()) - bn := strings.ToLower(b.Name()) - return cmp.Compare(an, bn) + return doNameSort(a, b) } func SortByNameReverse(a, b File) int { - an := strings.ToLower(a.Name()) - bn := strings.ToLower(b.Name()) - return cmp.Compare(bn, an) + return doNameSort(b, a) } func SortByPath(a, b File) int { @@ -105,6 +102,20 @@ func SortDirectoriesLast(a, b File) int { } } +func doNameSort(a, b File) int { + an := strings.ToLower(a.Name()) + bn := strings.ToLower(b.Name()) + // check if filename is a number + abase := strings.Replace(an, filepath.Ext(an), "", 1) + bbase := strings.Replace(bn, filepath.Ext(bn), "", 1) + ai, aerr := strconv.Atoi(abase) + bi, berr := strconv.Atoi(bbase) + if aerr == nil && berr == nil { + return cmp.Compare(ai, bi) + } + return cmp.Compare(an, bn) +} + func getSortingSize(f File) int64 { if f.IsDir() { return -1