-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsorting.go
More file actions
54 lines (49 loc) · 1.33 KB
/
Copy pathsorting.go
File metadata and controls
54 lines (49 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package rtapi
import (
"cmp"
"slices"
)
// Sorting selects a Torrent field and direction.
type Sorting int8
const (
DefaultSorting Sorting = iota
ByName
ByNameRev
ByDownRate
ByDownRateRev
ByUpRate
ByUpRateRev
BySize
BySizeRev
ByRatio
ByRatioRev
ByAge
ByAgeRev
ByUpTotal
ByUpTotalRev
)
// Sort orders torrents in place.
func (t Torrents) Sort(aSorting Sorting) {
switch aSorting {
case ByName, ByNameRev:
slices.SortFunc(t, func(a, b *Torrent) int { return cmp.Compare(a.Name, b.Name) })
case ByDownRate, ByDownRateRev:
slices.SortFunc(t, func(a, b *Torrent) int { return cmp.Compare(a.DownRate, b.DownRate) })
case ByUpRate, ByUpRateRev:
slices.SortFunc(t, func(a, b *Torrent) int { return cmp.Compare(a.UpRate, b.UpRate) })
case BySize, BySizeRev:
slices.SortFunc(t, func(a, b *Torrent) int { return cmp.Compare(a.Size, b.Size) })
case ByRatio, ByRatioRev:
slices.SortFunc(t, func(a, b *Torrent) int { return cmp.Compare(a.Ratio, b.Ratio) })
case ByAge, ByAgeRev:
slices.SortFunc(t, func(a, b *Torrent) int { return cmp.Compare(a.Age, b.Age) })
case ByUpTotal, ByUpTotalRev:
slices.SortFunc(t, func(a, b *Torrent) int { return cmp.Compare(a.UpTotal, b.UpTotal) })
default:
return
}
switch aSorting {
case ByNameRev, ByDownRateRev, ByUpRateRev, BySizeRev, ByRatioRev, ByAgeRev, ByUpTotalRev:
slices.Reverse(t)
}
}