aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2016-05-01 19:17:54 -0400
committerGrant Ammons <gammons@gmail.com>2016-05-01 19:17:54 -0400
commitb1fc3359eb7498236621d1aa6a9ff18f8cce73c0 (patch)
tree7422852d05743e195171c8006deca46a19e19e82
parent0905a863f50d9631e6c44488b952622e75d79785 (diff)
Sort todos by due date by default
-rw-r--r--todolist/app.go16
-rw-r--r--todolist/file_store.go12
-rw-r--r--todolist/todo_item.go12
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
+ }
+}