aboutsummaryrefslogtreecommitdiffstats
path: root/cache/sorting.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-02 16:36:48 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-02 16:36:48 +0200
commit0728c0050d82171659474900c407e1b6dcee43a5 (patch)
tree73473697a596bb0e93b30ab9a68fd25e7c3af98c /cache/sorting.go
parente3c445fa85db65b084580a38b919f17b8f8c2b68 (diff)
downloadgit-bug-0728c0050d82171659474900c407e1b6dcee43a5.tar.gz
cache: provide a generic bug sorting function
Diffstat (limited to 'cache/sorting.go')
-rw-r--r--cache/sorting.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/cache/sorting.go b/cache/sorting.go
new file mode 100644
index 00000000..eeeb315c
--- /dev/null
+++ b/cache/sorting.go
@@ -0,0 +1,73 @@
+package cache
+
+import "sort"
+
+type OrderBy int
+
+const (
+ _ OrderBy = iota
+ OrderById
+ OrderByCreation
+ OrderByEdit
+)
+
+type OrderDirection int
+
+const (
+ _ OrderDirection = iota
+ OrderAscending
+ OrderDescending
+)
+
+func (c *RepoCache) AllBugsId(order OrderBy, direction OrderDirection) []string {
+ if order == OrderById {
+ return c.orderIds(direction)
+ }
+
+ excerpts := c.allExcerpt()
+
+ var sorter sort.Interface
+
+ switch order {
+ case OrderByCreation:
+ sorter = BugsByCreationTime(excerpts)
+ case OrderByEdit:
+ sorter = BugsByEditTime(excerpts)
+ default:
+ panic("missing sort type")
+ }
+
+ if direction == OrderDescending {
+ sorter = sort.Reverse(sorter)
+ }
+
+ sort.Sort(sorter)
+
+ result := make([]string, len(excerpts))
+
+ for i, val := range excerpts {
+ result[i] = val.Id
+ }
+
+ return result
+}
+
+func (c *RepoCache) orderIds(direction OrderDirection) []string {
+ result := make([]string, len(c.excerpts))
+
+ i := 0
+ for key := range c.excerpts {
+ result[i] = key
+ i++
+ }
+
+ var sorter sort.Interface = sort.StringSlice(result)
+
+ if direction == OrderDescending {
+ sorter = sort.Reverse(sorter)
+ }
+
+ sort.Sort(sorter)
+
+ return result
+}