-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
63 lines (53 loc) · 1.52 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
package types
import (
"sort"
"time"
)
func SortTradesAscending(trades []Trade) []Trade {
sort.Slice(trades, func(i, j int) bool {
return trades[i].Time.Before(time.Time(trades[j].Time))
})
return trades
}
// SortOrdersAscending sorts by creation time ascending-ly
func SortOrdersAscending(orders []Order) []Order {
sort.Slice(orders, func(i, j int) bool {
return orders[i].CreationTime.Time().Before(orders[j].CreationTime.Time())
})
return orders
}
// SortOrdersDescending sorts by creation time descending-ly
func SortOrdersDescending(orders []Order) []Order {
sort.Slice(orders, func(i, j int) bool {
return orders[i].CreationTime.Time().After(orders[j].CreationTime.Time())
})
return orders
}
// SortOrdersByPrice sorts by creation time ascending-ly
func SortOrdersByPrice(orders []Order, descending bool) []Order {
var f func(i, j int) bool
if descending {
f = func(i, j int) bool {
return orders[i].Price.Compare(orders[j].Price) > 0
}
} else {
f = func(i, j int) bool {
return orders[i].Price.Compare(orders[j].Price) < 0
}
}
sort.Slice(orders, f)
return orders
}
// SortOrdersAscending sorts by update time ascending-ly
func SortOrdersUpdateTimeAscending(orders []Order) []Order {
sort.Slice(orders, func(i, j int) bool {
return orders[i].UpdateTime.Time().Before(orders[j].UpdateTime.Time())
})
return orders
}
func SortKLinesAscending(klines []KLine) []KLine {
sort.Slice(klines, func(i, j int) bool {
return klines[i].StartTime.Unix() < klines[j].StartTime.Unix()
})
return klines
}