add sort by extension
This commit is contained in:
parent
746bff694c
commit
689500c942
|
@ -3,6 +3,7 @@ package files
|
|||
|
||||
import (
|
||||
"cmp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -63,6 +64,16 @@ func SortByPathReverse(a, b File) int {
|
|||
return cmp.Compare(b.Path(), a.Path())
|
||||
}
|
||||
|
||||
func SortByExtension(a, b File) int {
|
||||
aext, bext := getExts(a, b)
|
||||
return cmp.Compare(aext, bext)
|
||||
}
|
||||
|
||||
func SortByExtensionReverse(a, b File) int {
|
||||
aext, bext := getExts(a, b)
|
||||
return cmp.Compare(bext, aext)
|
||||
}
|
||||
|
||||
func SortDirectoriesFirst(a, b File) int {
|
||||
if !a.IsDir() && b.IsDir() {
|
||||
return 1
|
||||
|
@ -82,3 +93,20 @@ func SortDirectoriesLast(a, b File) int {
|
|||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func getExts(a, b File) (string, string) {
|
||||
var aext, bext string
|
||||
as := strings.Split(a.Name(), ".")
|
||||
bs := strings.Split(b.Name(), ".")
|
||||
if len(as) <= 1 {
|
||||
aext = ""
|
||||
} else {
|
||||
aext = as[len(as)-1]
|
||||
}
|
||||
if len(bs) <= 1 {
|
||||
aext = ""
|
||||
} else {
|
||||
bext = bs[len(bs)-1]
|
||||
}
|
||||
return aext, bext
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@ const (
|
|||
PathReverse
|
||||
Size
|
||||
SizeReverse
|
||||
Extension
|
||||
ExtensionReverse
|
||||
Directory
|
||||
DirectoryReverse
|
||||
)
|
||||
|
@ -53,6 +55,10 @@ func (s Sorting) String() string {
|
|||
return "size"
|
||||
case SizeReverse:
|
||||
return "size (r)"
|
||||
case Extension:
|
||||
return "extension"
|
||||
case ExtensionReverse:
|
||||
return "extension (r)"
|
||||
case Directory:
|
||||
return "directories first"
|
||||
case DirectoryReverse:
|
||||
|
@ -80,6 +86,10 @@ func (s Sorting) Sorter() func(a, b files.File) int {
|
|||
return files.SortBySize
|
||||
case SizeReverse:
|
||||
return files.SortBySizeReverse
|
||||
case Extension:
|
||||
return files.SortByExtension
|
||||
case ExtensionReverse:
|
||||
return files.SortByExtensionReverse
|
||||
case Directory:
|
||||
return files.SortDirectoriesFirst
|
||||
case DirectoryReverse:
|
||||
|
|
Loading…
Reference in a new issue