diff --git a/internal/files/files.go b/internal/files/files.go index 94f6401..d8def04 100644 --- a/internal/files/files.go +++ b/internal/files/files.go @@ -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 +} diff --git a/internal/tables/sorting/sorting.go b/internal/tables/sorting/sorting.go index 43cbc2f..3058753 100644 --- a/internal/tables/sorting/sorting.go +++ b/internal/tables/sorting/sorting.go @@ -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: