-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsorting.go
More file actions
60 lines (55 loc) · 1.57 KB
/
Copy pathsorting.go
File metadata and controls
60 lines (55 loc) · 1.57 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
55
56
57
58
59
60
package rtapi
import (
"cmp"
"slices"
"strings"
)
// 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. Names compare case-insensitively, and the
// sort is stable: torrents that compare equal keep their order, in reverse
// sorts too.
func (t Torrents) Sort(aSorting Sorting) {
var compare func(a, b *Torrent) int
switch aSorting {
case ByName, ByNameRev:
compare = func(a, b *Torrent) int { return cmp.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)) }
case ByDownRate, ByDownRateRev:
compare = func(a, b *Torrent) int { return cmp.Compare(a.DownRate, b.DownRate) }
case ByUpRate, ByUpRateRev:
compare = func(a, b *Torrent) int { return cmp.Compare(a.UpRate, b.UpRate) }
case BySize, BySizeRev:
compare = func(a, b *Torrent) int { return cmp.Compare(a.Size, b.Size) }
case ByRatio, ByRatioRev:
compare = func(a, b *Torrent) int { return cmp.Compare(a.Ratio, b.Ratio) }
case ByAge, ByAgeRev:
compare = func(a, b *Torrent) int { return cmp.Compare(a.Age, b.Age) }
case ByUpTotal, ByUpTotalRev:
compare = func(a, b *Torrent) int { return cmp.Compare(a.UpTotal, b.UpTotal) }
default:
return
}
switch aSorting {
case ByNameRev, ByDownRateRev, ByUpRateRev, BySizeRev, ByRatioRev, ByAgeRev, ByUpTotalRev:
ascending := compare
compare = func(a, b *Torrent) int { return ascending(b, a) }
}
slices.SortStableFunc(t, compare)
}