diff options
| author | Grant Ammons <gammons@gmail.com> | 2016-05-01 19:17:54 -0400 |
|---|---|---|
| committer | Grant Ammons <gammons@gmail.com> | 2016-05-01 19:17:54 -0400 |
| commit | b1fc3359eb7498236621d1aa6a9ff18f8cce73c0 (patch) | |
| tree | 7422852d05743e195171c8006deca46a19e19e82 /todolist/file_store.go | |
| parent | 0905a863f50d9631e6c44488b952622e75d79785 (diff) | |
Sort todos by due date by default
Diffstat (limited to 'todolist/file_store.go')
| -rw-r--r-- | todolist/file_store.go | 12 |
1 files changed, 12 insertions, 0 deletions
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 } |
