diff options
| author | Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com> | 2018-12-13 23:45:58 +0300 |
|---|---|---|
| committer | Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com> | 2018-12-13 23:45:58 +0300 |
| commit | 4051d3ee15d6db397938957a952dc7749a32341e (patch) | |
| tree | 1d98aca5a18e73417cc6aabc9c510d072bd2c2d6 | |
| parent | migrating commands to go-git (diff) | |
| download | gitbatch-4051d3ee15d6db397938957a952dc7749a32341e.tar.gz | |
files sort added
| -rw-r--r-- | pkg/git/util-sort.go (renamed from pkg/git/util-repository-sort.go) | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/pkg/git/util-repository-sort.go b/pkg/git/util-sort.go index de5f454..e3067bc 100644 --- a/pkg/git/util-repository-sort.go +++ b/pkg/git/util-sort.go @@ -57,3 +57,42 @@ func (s LastModified) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s LastModified) Less(i, j int) bool { return s[i].ModTime.Unix() > s[j].ModTime.Unix() } + +// filesAlphabetical slice is the re-ordered *File slice that sorted according +// to alphabetical order (A-Z) +type filesAlphabetical []*File + +// Len is the interface implementation for Alphabetical sorting function +func (s filesAlphabetical) Len() int { return len(s) } + +// Swap is the interface implementation for Alphabetical sorting function +func (s filesAlphabetical) Swap(i, j int) { s[i], s[j] = s[j], s[i] } + +// Less is the interface implementation for Alphabetical sorting function +func (s filesAlphabetical) Less(i, j int) bool { + iRunes := []rune(s[i].Name) + jRunes := []rune(s[j].Name) + + max := len(iRunes) + if max > len(jRunes) { + max = len(jRunes) + } + + for idx := 0; idx < max; idx++ { + ir := iRunes[idx] + jr := jRunes[idx] + + lir := unicode.ToLower(ir) + ljr := unicode.ToLower(jr) + + if lir != ljr { + return lir < ljr + } + + // the lowercase runes are the same, so compare the original + if ir != jr { + return ir < jr + } + } + return false +} |
