blog/models/events/sorting.go

33 lines
764 B
Go
Raw Permalink Normal View History

package events
// ByDate sorts events by their start time.
type ByDate []*Event
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 {
2018-05-01 00:50:39 +00:00
return a[i].StartTime.Before(a[j].StartTime) || a[i].ID < a[j].ID
}
2018-05-12 03:15:16 +00:00
// ByName sorts RSVPs by name.
type ByName []RSVP
func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool {
var leftName, rightName string
if a[i].Contact.Name() != "" {
leftName = a[i].Contact.Name()
} else {
leftName = a[i].Name
}
if a[j].Contact.Name() != "" {
rightName = a[j].Contact.Name()
} else {
rightName = a[j].Name
}
return leftName < rightName
}