-
Notifications
You must be signed in to change notification settings - Fork 463
/
sort.go
64 lines (53 loc) · 1.45 KB
/
sort.go
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
61
62
63
64
package timeseries
import (
"sort"
"strconv"
)
// SortBy sorts series by value calculated with provided aggFunc in given order
func SortBy(series []*TimeSeriesData, order string, aggFunc AggFunc) []*TimeSeriesData {
aggregatedSeries := make([]TimeSeries, len(series))
for i, s := range series {
aggregatedSeries[i] = s.TS.GroupByRange(aggFunc)
}
// Sort by aggregated value
sort.Slice(series, func(i, j int) bool {
if len(aggregatedSeries[i]) > 0 && len(aggregatedSeries[j]) > 0 {
return *aggregatedSeries[i][0].Value < *aggregatedSeries[j][0].Value
} else if len(aggregatedSeries[j]) > 0 {
return true
}
return false
})
if order == "desc" {
reverseSeries := make([]*TimeSeriesData, len(series))
for i := 0; i < len(series); i++ {
reverseSeries[i] = series[len(series)-1-i]
}
series = reverseSeries
}
return series
}
func SortByItem(series []*TimeSeriesData) []*TimeSeriesData {
sort.Slice(series, func(i, j int) bool {
itemIDi, err := strconv.Atoi(series[i].Meta.Item.ID)
if err != nil {
return false
}
itemIDj, err := strconv.Atoi(series[j].Meta.Item.ID)
if err != nil {
return false
}
return itemIDi < itemIDj
})
return series
}
func SortByName(series []*TimeSeriesData, order string) []*TimeSeriesData {
sort.Slice(series, func(i, j int) bool {
if order == "desc" {
return series[i].Meta.Name > series[j].Meta.Name
} else {
return series[i].Meta.Name < series[j].Meta.Name
}
})
return series
}