diff --git a/model.go b/model.go index 89e5507..fabf2d8 100644 --- a/model.go +++ b/model.go @@ -1,6 +1,7 @@ package main import ( + "sort" "strings" "time" @@ -189,3 +190,41 @@ func (a *AppState) WriteDoneList() error { } return a.DoneList.WriteToFilename(a.getDoneFile()) } + +func (a *AppState) AdvancedPrioritySort(t1, t2 *todotxt.Task) bool { + if t1.HasPriority() && t2.HasPriority() { + return t1.Priority < t2.Priority + } else if t1.HasPriority() && !t2.HasPriority() { + return t1.Priority <= "Y" + } else if !t1.HasPriority() && t2.HasPriority() { + return "Y" <= t2.Priority + } + return t1.HasPriority() +} + +type customSort struct { + list todotxt.TaskList + by func(t1, t2 *todotxt.Task) bool +} + +func (c *customSort) Len() int { + return len(c.list) +} + +func (c *customSort) Swap(l, r int) { + c.list[l], c.list[r] = c.list[r], c.list[l] +} + +func (c *customSort) Less(l, r int) bool { + return c.by(&c.list[l], &c.list[r]) +} + +type By func(t1, t2 *todotxt.Task) bool + +func (by By) Sort(list []todotxt.Task) { + ts := &customSort{ + list: list, + by: by, + } + sort.Sort(ts) +} diff --git a/screen_main.go b/screen_main.go index 7e8c84a..4c90c71 100644 --- a/screen_main.go +++ b/screen_main.go @@ -116,8 +116,7 @@ func (screen *MainScreen) reloadList(bundle termboxScreen.Bundle) error { (*screen.displayList) = append(*screen.displayList, av) } } - screen.displayList.Sort(todotxt.SORT_CREATED_DATE_DESC) - screen.displayList.Sort(todotxt.SORT_PRIORITY_ASC) + By(app.AdvancedPrioritySort).Sort(*screen.displayList) case MainBundleListDone: if err := app.LoadDoneList(); err != nil { return err