59 lines
1.2 KiB
Go
Raw Normal View History

2019-08-29 03:23:58 +00:00
package arn
import "sort"
// Sort sorts the anime list by the given algorithm.
func (list *AnimeList) Sort(algorithm string) {
list.Lock()
defer list.Unlock()
switch algorithm {
case SortByTitle:
sort.Slice(list.Items, func(i, j int) bool {
a := list.Items[i]
b := list.Items[j]
return a.Anime().Title.Canonical < b.Anime().Title.Canonical
})
case SortByRating:
sort.Slice(list.Items, func(i, j int) bool {
a := list.Items[i]
b := list.Items[j]
if a.Rating.Overall == b.Rating.Overall {
return a.Anime().Title.Canonical < b.Anime().Title.Canonical
}
return a.Rating.Overall > b.Rating.Overall
})
case SortByAiringDate:
sort.Slice(list.Items, func(i, j int) bool {
a := list.Items[i]
b := list.Items[j]
epsA := a.Anime().UpcomingEpisode()
epsB := b.Anime().UpcomingEpisode()
if epsA == nil && epsB == nil {
if a.Rating.Overall == b.Rating.Overall {
return a.Anime().Title.Canonical < b.Anime().Title.Canonical
}
return a.Rating.Overall > b.Rating.Overall
}
if epsA == nil {
return false
}
if epsB == nil {
return true
}
return epsA.Episode.AiringDate.Start < epsB.Episode.AiringDate.Start
})
}
}