-
Notifications
You must be signed in to change notification settings - Fork 12
/
sort.go
119 lines (105 loc) · 3.03 KB
/
sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package todotxt
import (
"errors"
"sort"
"time"
)
// Flags for defining sort element and order.
const (
SORT_PRIORITY_ASC = iota
SORT_PRIORITY_DESC
SORT_CREATED_DATE_ASC
SORT_CREATED_DATE_DESC
SORT_COMPLETED_DATE_ASC
SORT_COMPLETED_DATE_DESC
SORT_DUE_DATE_ASC
SORT_DUE_DATE_DESC
)
// Sort allows a TaskList to be sorted by certain predefined fields.
// See constants SORT_* for fields and sort order.
func (tasklist *TaskList) Sort(sortFlag int) error {
switch sortFlag {
case SORT_PRIORITY_ASC, SORT_PRIORITY_DESC:
tasklist.sortByPriority(sortFlag)
case SORT_CREATED_DATE_ASC, SORT_CREATED_DATE_DESC:
tasklist.sortByCreatedDate(sortFlag)
case SORT_COMPLETED_DATE_ASC, SORT_COMPLETED_DATE_DESC:
tasklist.sortByCompletedDate(sortFlag)
case SORT_DUE_DATE_ASC, SORT_DUE_DATE_DESC:
tasklist.sortByDueDate(sortFlag)
default:
return errors.New("unrecognized sort option")
}
return nil
}
type tasklistSort struct {
tasklists TaskList
by func(t1, t2 *Task) bool
}
func (ts *tasklistSort) Len() int {
return len(ts.tasklists)
}
func (ts *tasklistSort) Swap(l, r int) {
ts.tasklists[l], ts.tasklists[r] = ts.tasklists[r], ts.tasklists[l]
}
func (ts *tasklistSort) Less(l, r int) bool {
return ts.by(&ts.tasklists[l], &ts.tasklists[r])
}
func (tasklist *TaskList) sortBy(by func(t1, t2 *Task) bool) *TaskList {
ts := &tasklistSort{
tasklists: *tasklist,
by: by,
}
sort.Sort(ts)
return tasklist
}
func (tasklist *TaskList) sortByPriority(order int) *TaskList {
tasklist.sortBy(func(t1, t2 *Task) bool {
if order == SORT_PRIORITY_ASC { // ASC
if t1.HasPriority() && t2.HasPriority() {
return t1.Priority < t2.Priority
}
return t1.HasPriority()
}
// DESC
if t1.HasPriority() && t2.HasPriority() {
return t1.Priority > t2.Priority
}
return !t1.HasPriority()
})
return tasklist
}
func sortByDate(asc bool, hasDate1, hasDate2 bool, date1, date2 time.Time) bool {
if asc { // ASC
if hasDate1 && hasDate2 {
return date1.Before(date2)
}
return hasDate2
}
// DESC
if hasDate1 && hasDate2 {
return date1.After(date2)
}
return !hasDate2
}
func (tasklist *TaskList) sortByCreatedDate(order int) *TaskList {
tasklist.sortBy(func(t1, t2 *Task) bool {
return sortByDate(order == SORT_CREATED_DATE_ASC, t1.HasCreatedDate(), t2.HasCreatedDate(), t1.CreatedDate, t2.CreatedDate)
})
return tasklist
}
func (tasklist *TaskList) sortByCompletedDate(order int) *TaskList {
tasklist.sortBy(func(t1, t2 *Task) bool {
return sortByDate(order == SORT_COMPLETED_DATE_ASC, t1.HasCompletedDate(), t2.HasCompletedDate(), t1.CompletedDate, t2.CompletedDate)
})
return tasklist
}
func (tasklist *TaskList) sortByDueDate(order int) *TaskList {
tasklist.sortBy(func(t1, t2 *Task) bool {
return sortByDate(order == SORT_DUE_DATE_ASC, t1.HasDueDate(), t2.HasDueDate(), t1.DueDate, t2.DueDate)
})
return tasklist
}