sort filenames by number
but only if filename is just a number ^[0-9]+\.({extension})$
This commit is contained in:
parent
1ac1f1c01f
commit
73a63db198
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue