Sort by file extensions (#230)

- preserve natural ordering of filenames if extensions are the same
  or are missing
- files without extensions rank higher on ascending sort and lower
  on descending sort
This commit is contained in:
Kennedy Mwenja authored and gokcehan committed 2019-10-04 21:44:52 +03:00
1 parent 41339a9e5a
commit 9991a40e87
4 files changed
+32 -1

No files matched your search

+26
View File
@@ -32,6 +32,7 @@ type file struct {
dirCount int
accessTime time.Time
changeTime time.Time
ext string
}
func readdir(path string) ([]*file, error) {
@@ -77,6 +78,10 @@ func readdir(path string) ([]*file, error) {
ct = lstat.ModTime()
}
// returns an empty string if extension could not be determined
// i.e. directories, filenames without extensions
ext := filepath.Ext(fpath)
files = append(files, &file{
FileInfo: lstat,
linkState: linkState,
@@ -84,6 +89,7 @@ func readdir(path string) ([]*file, error) {
dirCount: -1,
accessTime: at,
changeTime: ct,
ext: ext,
})
}
@@ -149,6 +155,26 @@ func (dir *dir) sort() {
sort.SliceStable(dir.files, func(i, j int) bool {
return dir.files[i].changeTime.Before(dir.files[j].changeTime)
})
case extSort:
sort.SliceStable(dir.files, func(i, j int) bool {
leftExt := strings.ToLower(dir.files[i].ext)
rightExt := strings.ToLower(dir.files[j].ext)
// if the extension could not be determined (directories, files without)
// use a zero byte so that these files can be ranked higher
if leftExt == "" {
leftExt = "\x00"
}
if rightExt == "" {
rightExt = "\x00"
}
// in order to also have natural sorting with the filenames
// combine the name with the ext but have the ext at the front
left := leftExt + strings.ToLower(dir.files[i].Name())
right := rightExt + strings.ToLower(dir.files[j].Name())
return left < right
})
}
if gOpts.sortType.option&reverseSort != 0 {