Change Sort Constants

This commit is contained in:
Brian Buller 2023-08-23 09:58:04 -05:00
parent c62767a5ef
commit b3e09780fb
2 changed files with 14 additions and 14 deletions

24
sort.go
View File

@ -8,23 +8,23 @@ import (
// Flags for defining sort element and order. // Flags for defining sort element and order.
const ( const (
SORT_UNFINISHED_START = iota SortUnfinishedStart = iota
SORT_START_DATE_ASC SortStartDateAsc
SORT_START_DATE_DESC SortStartDateDesc
SORT_FINISH_DATE_ASC SortFinishDateAsc
SORT_FINISH_DATE_DESC SortFinishDateDesc
SORT_ERROR SortError
) )
// Sort allows a TimerList to be sorted by certain predefined fields. // Sort allows a TimerList to be sorted by certain predefined fields.
// See constants SORT_* for fields and sort order. // See constants Sort* for fields and sort order.
func (timerlist *TimerList) Sort(sortFlag int) error { func (timerlist *TimerList) Sort(sortFlag int) error {
switch sortFlag { switch sortFlag {
case SORT_UNFINISHED_START: case SortUnfinishedStart:
timerlist.sortByUnfinishedThenStart() timerlist.sortByUnfinishedThenStart()
case SORT_START_DATE_ASC, SORT_START_DATE_DESC: case SortStartDateAsc, SortStartDateDesc:
timerlist.sortByStartDate(sortFlag) timerlist.sortByStartDate(sortFlag)
case SORT_FINISH_DATE_ASC, SORT_FINISH_DATE_DESC: case SortFinishDateAsc, SortFinishDateDesc:
timerlist.sortByFinishDate(sortFlag) timerlist.sortByFinishDate(sortFlag)
default: default:
return errors.New("Unrecognized sort option") return errors.New("Unrecognized sort option")
@ -81,14 +81,14 @@ func sortByDate(asc bool, date1, date2 time.Time) bool {
func (timerlist *TimerList) sortByStartDate(order int) *TimerList { func (timerlist *TimerList) sortByStartDate(order int) *TimerList {
timerlist.sortBy(func(t1, t2 *Timer) bool { timerlist.sortBy(func(t1, t2 *Timer) bool {
return sortByDate(order == SORT_START_DATE_ASC, t1.StartDate, t2.StartDate) return sortByDate(order == SortStartDateAsc, t1.StartDate, t2.StartDate)
}) })
return timerlist return timerlist
} }
func (timerlist *TimerList) sortByFinishDate(order int) *TimerList { func (timerlist *TimerList) sortByFinishDate(order int) *TimerList {
timerlist.sortBy(func(t1, t2 *Timer) bool { timerlist.sortBy(func(t1, t2 *Timer) bool {
return sortByDate(order == SORT_FINISH_DATE_ASC, t1.FinishDate, t2.FinishDate) return sortByDate(order == SortFinishDateAsc, t1.FinishDate, t2.FinishDate)
}) })
return timerlist return timerlist
} }

View File

@ -12,7 +12,7 @@ import (
) )
// TimerList represents a list of timer.txt timer entries. // TimerList represents a list of timer.txt timer entries.
// It is usually loasded from a whole timer.txt file. // It is usually loaded from a whole timer.txt file.
type TimerList struct { type TimerList struct {
timers []*Timer timers []*Timer
sortFlag int sortFlag int
@ -202,7 +202,7 @@ func (timerlist *TimerList) AddTimer(timer *Timer) {
// AddTimers adds all passed in timers to the list, sorts the list, then updates the Timer.Id values. // AddTimers adds all passed in timers to the list, sorts the list, then updates the Timer.Id values.
func (timerlist *TimerList) AddTimers(timers []*Timer) { func (timerlist *TimerList) AddTimers(timers []*Timer) {
timerlist.timers = append(timerlist.timers, timers...) timerlist.timers = append(timerlist.timers, timers...)
timerlist.Sort(SORT_START_DATE_ASC) timerlist.Sort(SortStartDateAsc)
} }
func (timerlist *TimerList) Combine(other *TimerList) { timerlist.AddTimers(other.timers) } func (timerlist *TimerList) Combine(other *TimerList) { timerlist.AddTimers(other.timers) }