Skip to content

Commit

Permalink
Added a more "natural" sort
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiu-persoiu committed Sep 16, 2019
1 parent eea7f89 commit 95286a8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions golisting.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"runtime"
"sort"
"sync"
"text/template"

Expand All @@ -24,6 +25,24 @@ type PageData struct {
Images []string
}

type byName []os.FileInfo

func (s byName) Len() int {
return len(s)
}

func (s byName) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func (s byName) Less(i, j int) bool {
if len(s[i].Name()) != len(s[j].Name()) {
return len(s[i].Name()) < len(s[j].Name())
}

return s[i].Name() < s[j].Name()
}

func main() {

path := flag.String("path", "./", "path to images")
Expand Down Expand Up @@ -64,6 +83,8 @@ func main() {

var images []string

sort.Sort(byName(files))

for _, f := range files {
ext := filepath.Ext(f.Name())
if ext == ".jpg" || ext == ".png" {
Expand Down

0 comments on commit 95286a8

Please sign in to comment.