go-timertxt/sort.go

107 lines
2.5 KiB
Go
Raw Normal View History

2019-02-15 18:46:50 +00:00
package timertxt
import (
"errors"
"sort"
"time"
)
// Flags for defining sort element and order.
const (
SORT_UNFINISHED_START = iota
SORT_START_DATE_ASC
2019-02-15 18:46:50 +00:00
SORT_START_DATE_DESC
SORT_FINISH_DATE_ASC
SORT_FINISH_DATE_DESC
2023-01-12 12:04:53 +00:00
SORT_ERROR
2019-02-15 18:46:50 +00:00
)
// Sort allows a TimerList to be sorted by certain predefined fields.
// See constants SORT_* for fields and sort order.
func (timerlist *TimerList) Sort(sortFlag int) error {
switch sortFlag {
case SORT_UNFINISHED_START:
timerlist.sortByUnfinishedThenStart()
2019-02-15 18:46:50 +00:00
case SORT_START_DATE_ASC, SORT_START_DATE_DESC:
timerlist.sortByStartDate(sortFlag)
case SORT_FINISH_DATE_ASC, SORT_FINISH_DATE_DESC:
timerlist.sortByFinishDate(sortFlag)
default:
return errors.New("Unrecognized sort option")
}
2023-01-12 12:04:53 +00:00
timerlist.sortFlag = sortFlag
2019-02-15 18:46:50 +00:00
return nil
}
2023-01-12 12:04:53 +00:00
func (timerlist *TimerList) refresh() {
timerlist.Sort(timerlist.sortFlag)
for i, t := range timerlist.timers {
t.Id = i
}
}
2019-02-15 18:46:50 +00:00
type timerlistSort struct {
timerlists TimerList
by func(t1, t2 *Timer) bool
}
func (ts *timerlistSort) Len() int {
2023-01-12 12:04:53 +00:00
return len(ts.timerlists.timers)
2019-02-15 18:46:50 +00:00
}
func (ts *timerlistSort) Swap(l, r int) {
2023-01-12 12:04:53 +00:00
ts.timerlists.timers[l], ts.timerlists.timers[r] = ts.timerlists.timers[r], ts.timerlists.timers[l]
2019-02-15 18:46:50 +00:00
}
func (ts *timerlistSort) Less(l, r int) bool {
2023-01-12 12:04:53 +00:00
return ts.by(ts.timerlists.timers[l], ts.timerlists.timers[r])
2019-02-15 18:46:50 +00:00
}
func (timerlist *TimerList) sortBy(by func(t1, t2 *Timer) bool) *TimerList {
ts := &timerlistSort{
timerlists: *timerlist,
by: by,
}
sort.Sort(ts)
return timerlist
}
func sortByDate(asc bool, date1, date2 time.Time) bool {
if asc { // ASC
if !date1.IsZero() && !date2.IsZero() {
return date1.Before(date2)
}
return !date2.IsZero()
}
// DESC
if !date1.IsZero() && !date2.IsZero() {
return date1.After(date2)
}
return date2.IsZero()
}
func (timerlist *TimerList) sortByStartDate(order int) *TimerList {
timerlist.sortBy(func(t1, t2 *Timer) bool {
return sortByDate(order == SORT_START_DATE_ASC, t1.StartDate, t2.StartDate)
})
return timerlist
}
func (timerlist *TimerList) sortByFinishDate(order int) *TimerList {
timerlist.sortBy(func(t1, t2 *Timer) bool {
return sortByDate(order == SORT_FINISH_DATE_ASC, t1.FinishDate, t2.FinishDate)
})
return timerlist
}
func (timerlist *TimerList) sortByUnfinishedThenStart() *TimerList {
timerlist.sortBy(func(t1, t2 *Timer) bool {
if t1.FinishDate.IsZero() && !t2.FinishDate.IsZero() {
return true
} else if t2.FinishDate.IsZero() && !t1.FinishDate.IsZero() {
return false
}
return sortByDate(false, t1.StartDate, t2.StartDate)
})
return timerlist
}