From 23645fe05f16b44c1d430c165fd0c2839e518eee Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Tue, 19 Sep 2017 11:37:03 +0300 Subject: 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 --- vendor/code.gitea.io/git/tree_entry.go | 40 ++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'vendor/code.gitea.io/git/tree_entry.go') 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 { -- cgit v1.2.3