diff options
author | Lauris BH <lauris@nix.lv> | 2017-09-19 11:37:03 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-19 11:37:03 +0300 |
commit | 23645fe05f16b44c1d430c165fd0c2839e518eee (patch) | |
tree | a0540625123b996edbe6a389ee578fadc3dfa236 /vendor/code.gitea.io/git | |
parent | 4cb9394a978af75794d43accc231b6649f2884bc (diff) | |
download | gitea-23645fe05f16b44c1d430c165fd0c2839e518eee.tar.gz gitea-23645fe05f16b44c1d430c165fd0c2839e518eee.zip |
Sort repository tree entries in natural way (#2506)
* Sort repository tree entries in natural way
* Fix sort for different length strings with first parts equal
* Improve test case
* Refactor return statements
* Update gitea/git dependency
Diffstat (limited to 'vendor/code.gitea.io/git')
-rw-r--r-- | vendor/code.gitea.io/git/tree_entry.go | 40 |
1 files changed, 28 insertions, 12 deletions
diff --git a/vendor/code.gitea.io/git/tree_entry.go b/vendor/code.gitea.io/git/tree_entry.go index 1e4934e81f..d5730a0d44 100644 --- a/vendor/code.gitea.io/git/tree_entry.go +++ b/vendor/code.gitea.io/git/tree_entry.go @@ -116,35 +116,51 @@ func (te *TreeEntry) GetSubJumpablePathName() string { // Entries a list of entry type Entries []*TreeEntry -var sorter = []func(t1, t2 *TreeEntry) bool{ - func(t1, t2 *TreeEntry) bool { +type customSortableEntries struct { + Comparer func(s1, s2 string) bool + Entries +} + +var sorter = []func(t1, t2 *TreeEntry, cmp func(s1, s2 string) bool) bool{ + func(t1, t2 *TreeEntry, cmp func(s1, s2 string) bool) bool { return (t1.IsDir() || t1.IsSubModule()) && !t2.IsDir() && !t2.IsSubModule() }, - func(t1, t2 *TreeEntry) bool { - return t1.name < t2.name + func(t1, t2 *TreeEntry, cmp func(s1, s2 string) bool) bool { + return cmp(t1.name, t2.name) }, } -func (tes Entries) Len() int { return len(tes) } -func (tes Entries) Swap(i, j int) { tes[i], tes[j] = tes[j], tes[i] } -func (tes Entries) Less(i, j int) bool { - t1, t2 := tes[i], tes[j] +func (ctes customSortableEntries) Len() int { return len(ctes.Entries) } + +func (ctes customSortableEntries) Swap(i, j int) { + ctes.Entries[i], ctes.Entries[j] = ctes.Entries[j], ctes.Entries[i] +} + +func (ctes customSortableEntries) Less(i, j int) bool { + t1, t2 := ctes.Entries[i], ctes.Entries[j] var k int for k = 0; k < len(sorter)-1; k++ { s := sorter[k] switch { - case s(t1, t2): + case s(t1, t2, ctes.Comparer): return true - case s(t2, t1): + case s(t2, t1, ctes.Comparer): return false } } - return sorter[k](t1, t2) + return sorter[k](t1, t2, ctes.Comparer) } // Sort sort the list of entry func (tes Entries) Sort() { - sort.Sort(tes) + sort.Sort(customSortableEntries{func(s1, s2 string) bool { + return s1 < s2 + }, tes}) +} + +// CustomSort customizable string comparing sort entry list +func (tes Entries) CustomSort(cmp func(s1, s2 string) bool) { + sort.Sort(customSortableEntries{cmp, tes}) } type commitInfo struct { |