From b1fc3359eb7498236621d1aa6a9ff18f8cce73c0 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Sun, 1 May 2016 19:17:54 -0400 Subject: Sort todos by due date by default --- todolist/app.go | 16 ++++++++-------- todolist/file_store.go | 12 ++++++++++++ todolist/todo_item.go | 12 ++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/todolist/app.go b/todolist/app.go index 74fb8dc..267f482 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -36,6 +36,14 @@ func (a *App) DeleteTodo(input string) { } } +func (a *App) ListTodos(input string) { + //filtered := NewFilter(a.TodoStore.Todos()).filter() + grouped := a.getGroups(input) + + formatter := NewFormatter(grouped) + formatter.Print() +} + func (a *App) getId(input string) int { re, _ := regexp.Compile("\\d+") @@ -47,14 +55,6 @@ func (a *App) getId(input string) int { } } -func (a *App) ListTodos(input string) { - //filtered := NewFilter(a.TodoStore.Todos()).filter() - grouped := a.getGroups(input) - - formatter := NewFormatter(grouped) - formatter.Print() -} - func (a *App) getGroups(input string) *GroupedTodos { grouper := &Grouper{} contextRegex, _ := regexp.Compile(`by c.*$`) diff --git a/todolist/file_store.go b/todolist/file_store.go index 992aeb3..579bf17 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "os/user" + "sort" ) type FileStore struct { @@ -73,7 +74,18 @@ func (f *FileStore) Save() { } } +type ByDate []Todo + +func (a ByDate) Len() int { return len(a) } +func (a ByDate) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByDate) Less(i, j int) bool { + t1Due := a[i].CalculateDueTime() + t2Due := a[j].CalculateDueTime() + return t1Due.Before(t2Due) +} + func (f *FileStore) Todos() []Todo { + sort.Sort(ByDate(f.Data)) return f.Data } diff --git a/todolist/todo_item.go b/todolist/todo_item.go index cc06615..6b820b1 100644 --- a/todolist/todo_item.go +++ b/todolist/todo_item.go @@ -1,5 +1,7 @@ package todolist +import "time" + type Todo struct { Id int `json:"id"` Subject string `json:"subject"` @@ -17,3 +19,13 @@ func NewTodo() *Todo { func (t Todo) Valid() bool { return (t.Subject != "") } + +func (t Todo) CalculateDueTime() time.Time { + if t.Due != "" { + parsedTime, _ := time.Parse("2006-01-02", t.Due) + return parsedTime + } else { + parsedTime, _ := time.Parse("2006-01-02", "1900-01-01") + return parsedTime + } +} -- cgit v1.3