mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
This turns locations into an ordered array, so we can have a 'natural' ordering This is backwards compatible Resolves #896
42 lines
803 B
Go
42 lines
803 B
Go
package clocks
|
|
|
|
import (
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
type ClockCollection struct {
|
|
Clocks []Clock
|
|
}
|
|
|
|
func (clocks *ClockCollection) Sorted(sortOrder string) []Clock {
|
|
if sortOrder == "natural" {
|
|
//no-op
|
|
} else if sortOrder == "chronological" {
|
|
clocks.SortedChronologically()
|
|
} else {
|
|
clocks.SortedAlphabetically()
|
|
}
|
|
|
|
return clocks.Clocks
|
|
}
|
|
|
|
func (clocks *ClockCollection) SortedAlphabetically() {
|
|
sort.Slice(clocks.Clocks, func(i, j int) bool {
|
|
clock := clocks.Clocks[i]
|
|
other := clocks.Clocks[j]
|
|
|
|
return clock.Label < other.Label
|
|
})
|
|
}
|
|
|
|
func (clocks *ClockCollection) SortedChronologically() {
|
|
now := time.Now()
|
|
sort.Slice(clocks.Clocks, func(i, j int) bool {
|
|
clock := clocks.Clocks[i]
|
|
other := clocks.Clocks[j]
|
|
|
|
return clock.ToLocal(now).String() < other.ToLocal(now).String()
|
|
})
|
|
}
|