]> git.lizzy.rs Git - go-anidb.git/blob - titles/sort.go
misc: refactor, add tests, add list simplification method
[go-anidb.git] / titles / sort.go
1 package titles
2
3 import (
4         "sort"
5 )
6
7 // Sorts the given Results.
8 func (cmp ResultComparer) Sort(res Results) {
9         sorter := &resultSorter{
10                 res: res,
11                 by:  cmp,
12         }
13         sort.Sort(sorter)
14 }
15
16 func (cmp ResultComparer) ReverseSort(res Results) {
17         sorter := &resultSorter{
18                 res: res,
19                 by:  cmp,
20         }
21         sort.Sort(sort.Reverse(sorter))
22 }
23
24 type resultSorter struct {
25         by  ResultComparer
26         res Results
27 }
28
29 func (f *resultSorter) Len() int {
30         return len(f.res)
31 }
32
33 func (f *resultSorter) Less(i, j int) bool {
34         return f.by(&f.res[i], &f.res[j])
35 }
36
37 func (f *resultSorter) Swap(i, j int) {
38         f.res[i], f.res[j] = f.res[j], f.res[i]
39 }