add sort by extension
This commit is contained in:
parent
746bff694c
commit
689500c942
2 changed files with 38 additions and 0 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue