From 1326d45fe527768c84e0df905664474b4b4b96bd Mon Sep 17 00:00:00 2001 From: Jan Hensel Date: Sun, 3 Dec 2023 11:36:14 +0100 Subject: [PATCH] fix(backlog): Sort tasks by deadline otherwise by category priority otherwise by category name otherwise by name --- internal/model/backlog.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/internal/model/backlog.go b/internal/model/backlog.go index 919e6f57..600ee292 100644 --- a/internal/model/backlog.go +++ b/internal/model/backlog.go @@ -3,6 +3,7 @@ package model import ( "fmt" "io" + "sort" "sync" "time" @@ -91,7 +92,7 @@ func (b *Backlog) Write(w io.Writer) error { return nil } -// Read reads and deserializes a backlog from the io.Reader and returns the +// BacklogFromReader reads and deserializes a backlog from the io.Reader and returns the // backlog. func BacklogFromReader(r io.Reader, categoryGetter func(string) Category) (*Backlog, error) { data, err := io.ReadAll(r) @@ -131,6 +132,9 @@ func BacklogFromReader(r io.Reader, categoryGetter func(string) Category) (*Back } } + // sort to ensure more consistent ordering + sort.Sort(TasksByDeadline(b.Tasks)) + return b, nil } @@ -277,21 +281,23 @@ func (t *Task) getDurationNormalized() time.Duration { } } -// NOTE: technically this following code is unused, it may be useful at some point though - -type ByDeadline []*Task +// TasksByDeadline is a sort interface to stort tasks by their deadlines +type TasksByDeadline []*Task -func (a ByDeadline) Len() int { return len(a) } -func (a ByDeadline) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a TasksByDeadline) Len() int { return len(a) } +func (a TasksByDeadline) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a ByDeadline) Less(i, j int) bool { +func (a TasksByDeadline) Less(i, j int) bool { switch { case a[i].Deadline == nil && a[j].Deadline == nil: // neither deadlines - if a[i].Category.Priority != a[i].Category.Priority { + if a[i].Category.Priority != a[j].Category.Priority { return a[i].Category.Priority > a[j].Category.Priority } - return true + if a[i].Category.Name != a[j].Category.Name { + return a[i].Category.Name < a[j].Category.Name + } + return a[i].Name < a[j].Name case a[i].Deadline == nil && a[j].Deadline != nil: // only second deadline return false